EFRepositoryBase.cs
using InjectExample.Model.ViewModel;
/* ==============================================================================
* 功能描述:EFRepositoryBase
* 创 建 者:蒲奎民
* 创建日期:2016-08-29 10:56:35
* CLR Version :4.0.30319.42000
* ==============================================================================*/
using System;
using System.Collections.Generic;
using System.Composition;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using EntityFramework.Extensions;
namespace InjectExample.Component
{
/// <summary>
/// EntityFramework仓储操作基类
/// </summary>
/// <typeparam name="TEntity">动态实体类型</typeparam>
public abstract class EfRepositoryBase<TEntity> : IRepository<TEntity> where TEntity : class //EntityBase<TKey>
{
#region 属性
/// <summary>
/// 获取 仓储上下文的实例
/// </summary>
[Import]
public IUnitOfWork UnitOfWork { get; set; }
/// <summary>
/// 获取 EntityFramework的数据仓储上下文
/// </summary>
protected UnitOfWorkContextBase EfContext
{
get
{
if (UnitOfWork is UnitOfWorkContextBase)
{
return UnitOfWork as UnitOfWorkContextBase;
}
return null;
// throw new DataAccessException(string.Format("数据仓储上下文对象类型不正确,应为UnitOfWorkContextBase,实际为 {0}", UnitOfWork.GetType().Name));
}
}
/// <summary>
/// 获取 当前实体的查询数据集,不跟踪状态
/// </summary>
public virtual IQueryable<TEntity> Entities
{
get { return EfContext.Set<TEntity>().AsNoTracking(); }
}
/// <summary>
/// 获取 当前实体的查询数据集,有跟踪状态
/// </summary>
public virtual IQueryable<TEntity> Table
{
get { return EfContext.Set<TEntity>(); }
}
#endregion
#region 公共方法
/// <summary>
/// 插入实体记录
/// </summary>
/// <param name="entity"> 实体对象 </param>
/// <param name="isSave"> 是否执行保存 </param>
/// <returns> 操作影响的行数 </returns>
public virtual int Insert(TEntity entity, bool isSave = true)
{
PublicHelper.CheckArgument(entity, "entity");
EfContext.RegisterNew(entity);
return isSave ? EfContext.Commit() : 0;
}
/// <summary>
/// 批量插入实体记录集合
/// </summary>
/// <param name="entities"> 实体记录集合 </param>
/// <param name="isSave"> 是否执行保存 </param>
/// <returns> 操作影响的行数 </returns>
public virtual int Insert(IEnumerable<TEntity> entities, bool isSave = true)
{
PublicHelper.CheckArgument(entities, "entities");
EfContext.RegisterNew(entities);
return isSave ? EfContext.Commit() : 0;
}
///// <summary>
///// 删除指定编号的记录
///// </summary>
///// <param name="id"> 实体记录编号 </param>
///// <param name="isSave"> 是否执行保存 </param>
///// <returns> 操作影响的行数 </returns>
//public virtual int Delete(object id, bool isSave = true)
//{
// PublicHelper.CheckArgument(id, "id");
// TEntity entity = EFContext.Set<TEntity>().Find(id);
// return entity != null ? Delete(entity, isSave) : 0;
//}
/// <summary>
/// 删除实体记录
/// </summary>
/// <param name="entity"> 实体对象 </param>
/// <param name="isSave"> 是否执行保存 </param>
/// <returns> 操作影响的行数 </returns>
public virtual int Delete(TEntity entity, bool isSave = true)
{
PublicHelper.CheckArgument(entity, "entity");
EfContext.RegisterDeleted<TEntity>(entity);
return isSave ? EfContext.Commit() : 0;
}
/// <summary>
/// 删除实体记录集合
/// </summary>
/// <param name="entities"> 实体记录集合 </param>
/// <param name="isSave"> 是否执行保存 </param>
/// <returns> 操作影响的行数 </returns>
public virtual int Delete(IEnumerable<TEntity> entities, bool isSave = true)
{
PublicHelper.CheckArgument(entities, "entities");
EfContext.RegisterDeleted<TEntity>(entities);
return isSave ? EfContext.Commit() : 0;
}
/// <summary>
/// 删除所有符合特定表达式的数据
/// </summary>
/// <param name="predicate"> 查询条件谓语表达式 </param>
/// <param name="isSave"> 是否执行保存 </param>
/// <returns> 操作影响的行数 </returns>
public virtual int Delete(Expression<Func<TEntity, bool>> predicate, bool isSave = true)
{
PublicHelper.CheckArgument(predicate, "predicate");
List<TEntity> entities = EfContext.Set<TEntity>().Where(predicate).ToList();
return entities.Count > 0 ? Delete(entities, isSave) : 0;
}
/// <summary>
/// 更新实体记录
/// </summary>
/// <param name="entity"> 实体对象 </param>
/// <param name="isSave"> 是否执行保存 </param>
/// <returns> 操作影响的行数 </returns>
public virtual int Update(TEntity entity, bool isSave = true)
{
PublicHelper.CheckArgument(entity, "entity");
EfContext.RegisterModified<TEntity>(entity);
return isSave ? EfContext.Commit() : 0;
}
public virtual int Update(IEnumerable<TEntity> entities, bool isSave = true)
{
PublicHelper.CheckArgument(entities, "entities");
EfContext.RegisterModified<TEntity>(entities);
return isSave ? EfContext.Commit() : 0;
}
/// <summary>
/// 提交更新
/// </summary>
/// <returns></returns>
public virtual int Commit(bool validateOnSaveEnabled = true)
{
return EfContext.Commit(validateOnSaveEnabled);
}
/// <summary>
/// 查找指定主键的实体记录
/// </summary>
&nbs