2013-07-26 186 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