[英]Calling a stored procedure in Entity Framework 6 Repository Pattern
我对实体框架(6)的熟悉程度略高于基本水平。 我基于从书本中得到的模式构建了一个系统。 我有效地使所有数据库调用都很好地通用。 但是,它没有解决调用存储过程的问题,而我有一些需要。 我完全确定如何根据此模式调用它们。
这是为访存建立的方式:
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; }
您应该能够在上下文中看到存储过程
using (var context = new EFRepository())
{
var ids= context.PropertiesContactIsInvolvedIn(id);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.