标签:oid objects def return man edits 方法 基础 invalid
查
public List<users> Querys() { datatestEntities db = new datatestEntities(); var a = db.users.ToList(); return a; } public users Querys(int id) { datatestEntities db = new datatestEntities(); var a = db.users.Find(id);//FirstOrDefault()返回序列中第一个元素 return a; }
改
public void Edits(EF.users m) { datatestEntities db = new datatestEntities(); db.Entry<users>(m).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); }
增
public void Inserts(users m) { datatestEntities db = new datatestEntities(); // var a = db.users.Add(m); db.Entry<users>(m).State = System.Data.Entity.EntityState.Added; db.SaveChanges(); }
删
public void Deletes(users m) { datatestEntities db = new datatestEntities(); //方法1 //db1.Entry<users>(m).State = System.Data.Entity.EntityState.Unchanged;//var a = db1.users.Remove(m);//System.InvalidOperationException:“无法删除此对象,因为未在 ObjectStateManager 中找到它。” //方法2 //db1.Entry<users>(m).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); }
ps:每次执行完之后记得SaveChanges()
仅供参考,有问题可评论指出。
标签:oid objects def return man edits 方法 基础 invalid
原文地址:https://www.cnblogs.com/xianyv/p/11704254.html