.NET / C# EF中的基础操作(CRUD)

        public List 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(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(m).State = System.Data.Entity.EntityState.Added;
            db.SaveChanges();
        }

        public void Deletes(users m)
        {
            datatestEntities db = new datatestEntities();
            //方法1
            //db1.Entry(m).State = System.Data.Entity.EntityState.Unchanged;//var a = db1.users.Remove(m);//System.InvalidOperationException:“无法删除此对象,因为未在 ObjectStateManager 中找到它。”
            //方法2
            //db1.Entry(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

仅供参考,有问题可评论指出。

 

你可能感兴趣的:(.NET / C# EF中的基础操作(CRUD))