DBContext.Set< T>不包含Set< T>的定义. [英] DBContext.Set<T> does not contain definition of Set<T>
本文介绍了DBContext.Set< T>不包含Set< T>的定义.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I am trying to implement an abstract repository pattern as described in THIS post. I'm getting the error message
'C'不包含'Set'的定义,也没有扩展方法 可以找到'Set'接受类型为'C'的第一个参数 缺少using指令或程序集引用?)
'C' does not contain a definition for 'Set' and no extension method 'Set' accepting a first argument of type 'C' could be found (are you missing a using directive or an assembly reference?)
其中C
是DBContext
where C
is the 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;
}
又一次更新
yet another update
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();
}
推荐答案
ObjectContext
没有Set
方法.它具有CreateObjectSet
方法
ObjectContext
does not have a Set
method. It has CreateObjectSet
method
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;
}
}
这篇关于DBContext.Set< T>不包含Set< T>的定义.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文