使用继承类的dbset

时间:2013-07-26 11:50:46

标签: asp.net-mvc asp.net-mvc-4 dbset

假设我有一个课程食物

public class FoodsContext : DbContext, IUnitOfWork
{
   public DbSet<Consumer> Consumers {get; set;}
}

并且有水果

public class FruitsContext: FoodsContext
{
   public DbSet<Price> Prices {get; set;}
}

然后在我的存储库中 可以说我有

public class SampleRepository
{
   private readonly FruitsContext _dbFruits = new FruitsContext();

   public void foo()
   {
      _dbFruits.Prices.doanything;
      //how can i use Consumers table that has been set in Foods class
   }
}

在我的存储库类中,我想访问使用者表中的值,而不创建 Food 类的实例。我怎么能这样做?

我在某个项目中看到了这个,但我现在还不太记得。有人可以建议吗?

2 个答案:

答案 0 :(得分:2)

它看起来像是对我的简单继承:

public void foo()
{
  _dbFruits.Prices.doanything;
  //how can i use Consumers table that has been set in Foods class
  _dbFruits.Consumers.doanything;  // this should work
}

答案 1 :(得分:0)

您也可以

public void foo()
{
  _dbFruits.Prices.doanything;
  //how can i use Consumers table that has been set in Foods class
  _dbFruits.Set<Consumer>.doanything;
}