ef6.0增删改查操作

案例代码如下:

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<LiuYan>(liy);
liuyanupdate.State = EntityState.Unchanged;
liuyanupdate.Property("Bh").IsModified = true;
liuyanupdate.Property("qq").IsModified = true;
ef.SaveChanges();

 

//无查询直接删除数据
liuyanupdate.State = EntityState.Deleted;
ef.SaveChanges();

posted @ 2018-11-03 09:53  飞刀软件  阅读(965)  评论(0编辑  收藏  举报