2013-07-26 122 views
0

的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 
    } 
} 

在我的存储库类中,我想访问消费者的值表,而不创建食物类的实例。我怎样才能做到这一点?

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

回答

0

你也可以做

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

是的,这是一个的DbContext方法。另一个基类。 –

+0

但是,请审查您的设计,也许复制一些制定出来的东西更多。 UnitOfWork通常拥有1个或更多的存储库,它不是它们的基类。 –

+0

其实我的设计更复杂,我只想在这里展示基本的随机课程。 – Cybercop

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 
} 
+0

这可以工作,但我已经发布了一个答案,还曾 – Cybercop