我有一个通用存储库和 UoW,我模仿尊敬的 Dykstra 先生的圣经帐户:http ://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository- and-unit-of-work-patterns-in-an-asp-net-mvc-application。
namespace Bookstore.DAL
{
public class GenericRepository<TEntity> where TEntity : class
{
internal BookstoreContext context;
internal DbSet<TEntity> dbSet;
public GenericRepository(BookstoreContext context)
{
this.context = context;
dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
query = includeProperties.Split(new[] {','},
StringSplitOptions.RemoveEmptyEntries).
Aggregate(query, (current, includeProperty) => current.Include(includeProperty));
return orderBy != null ? orderBy(query).ToList() : query.ToList();
}
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
public virtual void Delete(object id)
{
var entityToDelete = dbSet.Find(id);
Delete(entityToDelete);
}
public virtual void Delete(TEntity entityToDelete)
{
if (context.Entry(entityToDelete).State == EntityState.Detached)
{
dbSet.Attach(entityToDelete);
}
dbSet.Remove(entityToDelete);
}
public virtual void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
}
}
我需要做的是重写插入方法以使用我在上下文类中定义好的存储过程。我可以轻松绕过 UoW 并构建一个直接从上下文中使用 SP 的控制器,但这并不是最优的。
我的问题是: 如何重写方法以使用在上下文中定义的存储过程?