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