using System; using System.Linq; using System.Linq.Expressions; using YLShipBuildLandMap.Entity; namespace YLShipBuildLandMap.Services.Common { public class BaseService where TEntity : class { protected YLShipBuildLandMapContext DbContext { get; set; } public BaseService(YLShipBuildLandMapContext dbContext) { DbContext = dbContext; } public virtual TEntity Get(object keyValue) { return this.DbContext.Find(new object[] { keyValue }); } public virtual int Delete(Expression> predicate) { this.DbContext.Set().Where(predicate).DeleteFromQuery(); return this.DbContext.SaveChanges(); } public virtual int Update(object keyValue, Func updateFunc) { var entity = this.Get(keyValue); if (entity == null) { this.DbContext.Add(updateFunc(entity)); } else { this.DbContext.Update(updateFunc(entity)); }; return this.DbContext.SaveChanges(); } public virtual int UpdateFromQuery(Expression> predicate, Expression> updateFunc) { this.DbContext.Set().Where(predicate).UpdateFromQuery(updateFunc); return this.DbContext.SaveChanges(); } } }