查
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(); }
EF+lambda表达式 实现LIKE模糊查询
s => s.XianWID.StartsWith(str) 匹配以str开头的 s => s.XianWID.EndsWith(str) 匹配以str结尾的 s => s.XianWID.Contains(str) 匹配包含str的
ps:每次执行完之后记得SaveChanges();以上内容基于EF6
仅供参考,有问题可评论指出。