案例代碼如下:
Model1Container ef = new Model1Container();
//查詢
var liuyan = ef.LiuYan.Where(m => m.BH == "1").FirstOrDefault();
//查詢後更新
liuyan.QQ = "1183816006";
ef.SaveChanges();
//插入
var liy = new LiuYan();
ef.LiuYan.Add(liy);
ef.SaveChanges();
//無查詢直接附加全字段更新
ef.LiuYan.Attach(liy);
ef.SaveChanges();
//按需更新指定字段
var liuyanupdate= ef.Entry(liy);
liuyanupdate.State = EntityState.Unchanged;
liuyanupdate.Property("Bh").IsModified = true;
liuyanupdate.Property("qq").IsModified = true;
ef.SaveChanges();
//無查詢直接删除資料
liuyanupdate.State = EntityState.Deleted;
ef.SaveChanges();