EF6 简单增删改查示例代码

示例一:

private DbContext _dbContext;  
       public DbContext CurrentContext  
       {  
           get  
           {  
               if (_dbContext == null)  
               {  
                   _dbContext = new TransitContext();  
               }  
               return _dbContext;  
           }  
       }  
  
       ///   
       /// 执行Sql查询  
       ///   
       ///   
       ///   
       ///   
       ///   
       public List SqlQuery(string strSql, params Object[] paramObjects) where TEntity : class  
       {  
           if (paramObjects == null)  
           {  
               paramObjects = new object[0];  
           }  
           return this.CurrentContext.Database.SqlQuery(strSql, paramObjects).ToList();  
       }  
  
       ///   
       /// 查询  
       ///   
       ///   
       ///   
       public IQueryable Search(Expression> predicate) where TEntity : class  
       {  
           return CurrentContext.Set().Where(predicate);  
       }  
  
       ///   
       /// 查询全部  
       ///   
       ///   
       public IQueryable FindAll() where TEntity : class  
       {  
           return CurrentContext.Set();  
       }  
  
       ///   
       /// 插入  
       ///   
       ///   
       ///   
       public void Insert(TEntity entity, bool isSave = true) where TEntity : class  
       {  
           CurrentContext.Set().Add(entity);  
           if (isSave)  
           {  
               CurrentContext.SaveChanges();  
           }  
       }  
  
       ///   
       /// 批量插入  
       ///   
       ///   
       ///   
       public void Insert(List entitys, bool isSave = true) where TEntity : class  
       {  
           foreach (var entity in entitys)  
           {  
               CurrentContext.Set().Add(entity);  
           }  
           if (isSave)  
           {  
               CurrentContext.SaveChanges();  
           }  
       }  
  
       ///   
       /// 更新  
       ///   
       ///   
       ///   
       public void Update(TEntity entity, bool isSave = true) where TEntity : class  
       {  
           var local = FindLocal(CurrentContext, entity);  
           if (local == null)  
           {  
               throw new Exception("要更新的实体不存在");  
           }  
           ObjectMapper.CopyProperties(entity, local);  
           if (isSave)  
           {  
               CurrentContext.SaveChanges();  
           }  
       }  
  
       ///   
       /// 批量更新  
       ///   
       ///   
       ///   
       public void Update(List entities, bool isSave = true) where TEntity : class  
       {  
           foreach (var entity in entities)  
           {  
               var local = FindLocal(CurrentContext, entity);  
               if (local == null)  
               {  
                   throw new Exception("要更新的实体不存在");  
               }  
               ObjectMapper.CopyProperties(entity, local);  
           }  
           if (isSave)  
           {  
               CurrentContext.SaveChanges();  
           }  
       }  
  
       ///   
       /// 删除  
       ///   
       ///   
       ///   
       public void Delete(TEntity entity, bool isSave = true) where TEntity : class  
       {  
           var local = FindLocal(CurrentContext, entity);  
           if (local == null)  
           {  
               throw new Exception("要删除的实体不存在");  
           }  
           CurrentContext.Entry(local).State = EntityState.Deleted;  
           if (isSave)  
           {  
               CurrentContext.SaveChanges();  
           }  
       }  

  

 

示例二:

 

EF6 简单增删改查示例代码_第1张图片

你可能感兴趣的:(EF6 简单增删改查示例代码)