调用实体框架6存储库模式中的存储过程

本文关键字:模式 存储过程 存储 实体 框架 调用 | 更新日期: 2023-09-27 18:13:25

我对实体框架(6)的熟悉程度略高于基本水平。我根据从一本书中得到的模式建立了一个系统。它有效地使所有db调用都很通用。然而,它没有解决调用存储过程的问题,我有一些我需要的。根据这种模式,我很清楚该怎么称呼他们。

这是如何建立Fetches:

public class EFRepository<T> : IRepository<T> where T : class
{
    public EFRepository(DbContext dbContext)
    {
        if (dbContext == null)
            throw new ArgumentNullException("dbContext");
        DbContext = dbContext;
        DbSet = DbContext.Set<T>();
    }
    protected DbContext DbContext { get; set; }
    protected DbSet<T> DbSet { get; set; }
    public virtual IQueryable<T> GetAll()
    {
        return DbSet;
    }
    public virtual IQueryable<T> GetTop50()
    {
        return DbSet.Take(50);
    }
    public virtual T GetById(int id)
    {
        return DbSet.Find(id);
    }
}

所以,我想问题是,我将如何进行调用,传递存储过程的名称和参数?结果还是一个DbSet吗?存储过程在EF中建立

public virtual ObjectResult<PropertiesContactIsInvolvedIn_Result> PropertiesContactIsInvolvedIn(Nullable<int> contactID)
{
    var contactIDParameter = contactID.HasValue ?
            new ObjectParameter("ContactID", contactID) :
            new ObjectParameter("ContactID", typeof(int));
    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<PropertiesContactIsInvolvedIn_Result>("PropertiesContactIsInvolvedIn", contactIDParameter);
}

另外,正如我所说的,这里涉及到一个存储库方法,实体是这样表示的:

public IRepository<PropertiesContactIsInvolvedIn_Result> PropertiesContactIsInvolvedIn { get { return GetStandardRepo<PropertiesContactIsInvolvedIn_Result>(); } }
IRepository<PropertiesContactIsInvolvedIn_Result> PropertiesContactIsInvolvedIn { get; }

调用实体框架6存储库模式中的存储过程

您应该能够在上下文中看到存储过程

using (var context = new EFRepository())
{
var ids= context.PropertiesContactIsInvolvedIn(id);  

}