/// <summary> /// 新增实体 /// </summary> /// <param name="entity"></param> /// <returns></returns> public virtual int Add(T entity) { using (var dbContext = new DcContext()) { entity.CreateTime = DateTime.Now; dbContext.Set <T>().Add(entity); return(dbContext.SaveChanges()); } }
public virtual int Remove(T entity) { using (var dbContext = new DcContext()) { if (!dbContext.Set <T>().Local.Contains(entity)) { dbContext.Set <T>().Attach(entity); } dbContext.Set <T>().Remove(entity); return(dbContext.SaveChanges()); } }
/// <summary> /// 修改实体 /// </summary> /// <param name="entity"></param> /// <returns></returns> public virtual int Update(T entity) { using (var dbContext = new DcContext()) { if (!dbContext.Set <T>().Local.Contains(entity)) { dbContext.Set <T>().Attach(entity); } entity.UpdateTime = DateTime.Now; dbContext.Entry(entity).State = EntityState.Modified; return(dbContext.SaveChanges()); } }
/// <summary> /// 删除实体 /// </summary> /// <param name="entity"></param> /// <returns></returns> public virtual int Delete(T entity) { using (var dbContext = new DcContext()) { if (!dbContext.Set <T>().Local.Contains(entity)) { dbContext.Set <T>().Attach(entity); } (entity as ISoftDelete).IsDeleted = true; dbContext.Entry <T>(entity).State = EntityState.Modified; return(dbContext.SaveChanges()); } }
public virtual int Add(IEnumerable <T> entities /*, bool isUseBulkInsert = false*/) { using (var dbContext = new DcContext()) { //if (isUseBulkInsert) //{ // dbContext.BulkInsert(entities); //} //else //{ foreach (var entity in entities) { dbContext.Set <T>().Add(entity); } //} return(dbContext.SaveChanges()); } }
/// <summary> /// 批量删除实体 by some where /// </summary> /// <param name="entity"></param> /// <returns></returns> public virtual int Deletes(Expression <Func <T, bool> > where) { using (var dbContext = new DcContext()) { foreach (var entity in dbContext.Set <T>().Where(where)) { if (entity is ISoftDelete) { (entity as ISoftDelete).IsDeleted = true; dbContext.Entry <T>(entity).State = EntityState.Modified; } else { dbContext.Set <T>().Remove(entity); } } return(dbContext.SaveChanges()); } }