public void Add(T entity, bool isSaved = true) { if (string.IsNullOrEmpty(entity.Id)) { entity.Id = Guid.NewGuid().ToString(); } _context.Set <T>().Add(entity); if (isSaved) { _context.SaveChanges(); } _context.Entry(entity).State = EntityState.Detached; }
///// <summary> ///// 得到分页记录 ///// </summary> ///// <param name="pageindex">The pageindex.</param> ///// <param name="pagesize">The pagesize.</param> ///// <param name="orderby">排序,格式如:"Id"/"Id descending"</param> //public IQueryable<T> Find<T>(int pageindex, int pagesize, string orderby = "", Expression<Func<T, bool>> exp = null) where T : class //{ // if (pageindex < 1) pageindex = 1; // if (string.IsNullOrEmpty(orderby)) // orderby = "Id descending"; // return Filter(exp).OrderBy(orderby).Skip(pagesize * (pageindex - 1)).Take(pagesize); //} ///// <summary> ///// 根据过滤条件获取记录数 ///// </summary> //public int GetCount<T>(Expression<Func<T, bool>> exp = null) where T : class //{ // return Filter(exp).Count(); //} //public void Add<T>(T entity) where T : Entity //{ // if (string.IsNullOrEmpty(entity.Id)) // { // entity.Id = Guid.NewGuid().ToString(); // } // _context.Set<T>().Add(entity); //} ///// <summary> ///// 批量添加 ///// </summary> ///// <param name="entities">The entities.</param> //public void BatchAdd<T>(T[] entities) where T : Entity //{ // foreach (var entity in entities) // { // entity.Id = Guid.NewGuid().ToString(); // } // _context.Set<T>().AddRange(entities); //} //public void Update<T>(T entity) where T:class //{ // var entry = this._context.Entry(entity); // entry.State = EntityState.Modified; // //如果数据没有发生变化 // if (!this._context.ChangeTracker.HasChanges()) // { // entry.State = EntityState.Unchanged; // } //} //public void Delete<T>(T entity) where T:class //{ // _context.Set<T>().Remove(entity); //} ///// <summary> ///// 实现按需要只更新部分更新 ///// <para>如:Update(u =>u.Id==1,u =>new User{Name="ok"});</para> ///// </summary> ///// <param name="where">The where.</param> ///// <param name="entity">The entity.</param> //public void Update<T>(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity) where T:class //{ // _context.Set<T>().Where(where).Update(entity); //} //public virtual void Delete<T>(Expression<Func<T, bool>> exp) where T : class //{ // _context.Set<T>().RemoveRange(Filter(exp)); //} public bool Save() { //try //{ return(_context.SaveChanges() > 0); //} //catch (DbEntityValidationException e) //{ // throw new Exception(e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage); //} }