我無法找到一種方法從數據庫及其所有相關對象中加載對象。關於此簡化模型(ID-屬性未示出):EF:加載相關對象的相關對象
class MainClass
{
public virtual ICollection<FirstLevelClass> FirstLevelObjects {get; set;}
}
class FirstLevelClass
{
public virtual ICollection<SecondLevelClassA> SecondLevelObjectsA {get; set;}
public virtual ICollection<SecondLevelClassB> SecondLevelObjectsB {get; set;}
}
class SecondLevelClassA
{
public virtual int SomeValue {get; set;}
}
class SecondLevelClassB
{
public virtual int SomeValue {get; set;}
}
的的DbContext是 「MainClass」 -Objects:
public SampleContext : DbContext
{
public DbSet<MainClass> MainClassObjects {get; set;}
}
我怎樣才能加載MainClass對象從分貝與所有第一方和二級對象?我可以這樣做:
using (var context = new SampleContext())
{
var MainClassObjects = context.MainClassObjects.Include(p => p.FirstLevelObjects).ToList();
// ...move objects from the context to program logic...
}
但是我如何獲得SecondLevelObjects? I'm失去了一些東西,如:
using (var context = new SampleContext())
{
var MainClassObjects = context.MainClassObjects.Include(p => p.FirstLevelObjects.SecondLevelObjects).ToList();
// ...move objects from the context to program logic...
}
這甚至可能還是我必須適應在的DbContext的DbSets?
事實上,如果你使用行Include(「FirstLevelObjects.SecondLevelObjects」) 你甚至不需要使用Include(「FirstLevelObjects」) – ElDog
我有點不確定,謝謝你的澄清。 – VahidNaderi
我甚至曾嘗試過,但它根本沒有工作。我甚至不能只包含FirstLevelObjects。也許我錯過了一些東西,我會再試一次。 – tafkab76