EF CRUD 操作

1.Add 操作

        public bool Add(EFDataModels.User model)
        {
            try
            {
	       int result=0;
                using (DBEntities db = new DBEntities())
                {
                    if (model != null)
                    {
                        db.User.Add(model); // 添加模型
                        result = db.SaveChanges();  //保存到数据库
                    }
                }
                 return result > 0;  //返回结果
            }
            catch (Exception e)
            {
                throw e;
            }
        }

 2.Update 操作

 public bool Update(User model)
        {
            try
            {
                using (DBEntities db = new DBEntities())
                {

		db.Entry<User>(model).State = System.Data.Entity.EntityState.Modified; //设置模型的状态
		int result = db.SaveChanges(); // 保存更新
		return result > 0;
                   
                }
                return false;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

 3. Delete  操作

        public bool Delete(int id)
        {
            try
            {
                using (DBEntities db = new DBEntities())
                {
                    var model = (from p in db.User
                                 where p.ID == id
                                 select p).FirstOrDefault<User>();
                    if (model != null)
                    {
                        db.User.Attach(model);
                        db.User.Remove(model); // 从上下文中移除 模型
                        db.Entry<User>(model).State = System.Data.Entity.EntityState.Deleted;
                        int result = db.SaveChanges();
                        return result > 0;
                    }
                }
                return false;
            }
            catch (Exception e)
            {
                throw e;
            

4. Select 操作

        public User GetModel(int id)
        {
            try
            {
                using (DBEntities db = new DBEntities())
                {
                    var model = (from p in db.User
                                 where p.ID == id
                                 select p).FirstOrDefault<User>();
                    return model;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

5. 分页

 public List<UserModel> GetData(int pageIndex, int pageSize,  out int totalPage)
        {
            try
            {
                using (DBEntities db = new DBEntities())
                {

                    IQueryable<UserModel> quaryable = 
	            from c in  db.User
                    select c.ID !=0
                ;
                   
                   int totalCount = quaryable.Count();
                   var mylist = quaryable.OrderBy(p=>p.ID).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                   totalPage = (totalCount - 1) / pageSize + 1;
                    return mylist;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
原文地址:https://www.cnblogs.com/wisdo/p/4922138.html