0

可以说我有一堂食物

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类的实例。我怎样才能做到这一点?

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

4

2 回答 2

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
}
于 2013-07-26T12:35:30.547 回答
0

你也可以做

public void foo()
{
  _dbFruits.Prices.doanything;
  //how can i use Consumers table that has been set in Foods class
  _dbFruits.Set<Consumer>.doanything;
}
于 2013-07-26T12:39:00.707 回答