我正在尝试实现THIS帖子中描述的抽象存储库模式。我收到了错误消息
' C'不包含' Set'的定义没有扩展方法 '设置'接受第一个类型' C'可以找到(是你 缺少using指令或程序集引用?)
其中C
是DBContext
namespace Rental.Data.Entity.Repository
{
public abstract class GenericRepo<C, T> :
IGenericRepo<T> where T : class where C : RentalContainer, new()
{
private C _DBContext = new C();
protected C DBContext
{
get { return _DBContext; }
set { _DBContext = value; }
}
public virtual IQueryable<T> GetAll()
{
IQueryable<T> query = _DBContext.Set<T>(); <-- here is gives the error
return query;
}
又一次更新
public partial class RentalContainer : ObjectContext
{
#region Constructors
/// <summary>
/// Initializes a new RentalContainer object using the connection string found in the 'RentalContainer' section of the application configuration file.
/// </summary>
public RentalContainer() : base("name=RentalContainer", "RentalContainer")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
/// <summary>
/// Initialize a new RentalContainer object.
/// </summary>
public RentalContainer(string connectionString) : base(connectionString, "RentalContainer")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
答案 0 :(得分:3)
ObjectContext
没有Set
方法。它有CreateObjectSet
方法
public abstract class GenericRepo<C, T> : IGenericRepo<T>
where T : class
where C : RentalContainer, new()
{
private C _DBContext = new C();
protected C DBContext
{
get { return _DBContext; }
set { _DBContext = value; }
}
public virtual IQueryable<T> GetAll()
{
IQueryable<T> query = _DBContext.CreateObjectSet<T>();
return query;
}
}
答案 1 :(得分:2)
添加对EntityFramework.dll的引用
http://msdn.microsoft.com/en-us/library/gg679544(v=vs.103).aspx
答案 2 :(得分:0)
确保您的DataContext类扩展了DBContext