實體數據模型的增加與刪除
首先創建edmx 文件 從數據庫創建模型或自己創建模型生成數據庫。
然後找到關聯,如果沒有關聯表則自己關聯 並在關聯裏選擇級聯刪除(end on delete ->cascade)
表topic
表reply( *(reply)----->(1,0)topic )
表toreply ( *(toreply)----->(1,0)reply )
最後在創建的cs文件裏使用
增加
var model = new BBSToReply();
model.UserId = WebSecurity.CurrentUserId;
model.ToUser = int.Parse(form["touser"]);
model.DateTime = DateTime.Now;
db.BBSToReply.Add(model);
db.SaveChanges();
刪除
int topicid = int.Parse(form["topicid"]);//獲取id
var item = db.BBSTopic.Where(m => m.Tid == topicid).First();//找到該條目
var replytemp = item.BBSReply.All(m=>m.BBSToReply.Count!=0);//向後遍歷出toreplyli複合條件的
db.BBSTopic.Remove(item);
db.SaveChanges();
修改
可使用