DBContext.Set <t>不包含Set <t> </t> </t>的定义

时间:2012-02-12 11:36:45

标签: asp.net-mvc-3 entity-framework

我正在尝试实现THIS帖子中描述的抽象存储库模式。我收到了错误消息

  

&#39; C&#39;不包含&#39; Set&#39;的定义没有扩展方法   &#39;设置&#39;接受第一个类型&#39; C&#39;可以找到(是你   缺少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();
        }

3 个答案:

答案 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)

答案 2 :(得分:0)

确保您的DataContext类扩展了DBContext