2015-11-05 104 views
2

我有一个名为Address的实体。一个地址包含一个名为House的复杂类型。房子包含对其居住者的参考。乘客是一个实体。复杂类型引用的EagerLoad实体

public class Address { 

    [key] 
    public int Id { get; set; } 

    public House House { get; set; } 
} 

家:

[ComplexType] 
public class House 
{ 

    [Required] 
    public string HouseType { get; set; } 


    public IList<Occupant> Occupants { get; set; } 
} 

乘员

public class Occupant 
{ 

    [key] 
    public int Id { get; set; } 

    [Required] 
    public string Name { get; set; } 

    public virtual Address Address { get; set; } 

} 

如果我使用惰性加载一切工作正常,我可以访问所有属性。但是我需要使用EagerLoading,因为在处理Context之后很长时间需要实体。

我已经tryed,包括使用此代码的属性:

// DbSet is of type DbSet<Address> 
    List<Address> eagerLoadedEntity = DbSet.Where(a => a.Address.StartsWith("a")) 
       .Include(a => a.House.Occupants).ToList(); 

我得到以下错误:

A specified Include path is not valid. The EntityType 'Address' does not declare a navigation property with the name 'House'.

+0

是什么类型'DbSet'? 'DbSet

'? – flindeberg

+0

哦....等等.....你有一个地址类内的地址类.... 也许你想在那里的房子? –

+0

@AnestisKivranoglou是的,我想要那里的房子。 – CodeTower

回答

2

也许这是不可能的呢? MSDN Complex Types

Complex types cannot participate in associations and cannot contain navigation properties.

你是治疗“居住者”作为“包含”声明“豪斯医生”的导航性能,我想这可能是问题。

1

这是可能的

检查SO link to how to do nested eagerLoading

是这样的:这只是没有测试......你将需要调整的例子。

List<Address> eagerLoadedEntity = Context.Addresses 
         .Include("House") 
         .Include("House.Occupants") 
         .Where(a => a.Address.StartsWith("a")) 
         .ToList(); 

更新

对不起,我想你大概复合类型也许正确的....但如果他们所有的数据库实体,那么你应该能够做到......像......只是一个FYI

public class Address 
{ 
    public int Id {get; set;} 

    public int HouseId {get; set;} 

    public string AddressLine1 { get; set;} 

    public House House {get; set;} 
} 
public class House 
{ 
    public int Id {get; set;} 
    public string HouseType {get; set;} 

    public virtual ICollection<Occupant> Occupants { get; set;} 
} 

public class Occupant 
{ 
    public int Id {get; set;} 

    public int HouseId {get; set;} 
    public int PersonId {get; set;} 

    public bool IsOwner {get; set;} 
    public DateTime StartDate {get; set;} 
    public DateTime EndDate {get; set;} 

    public Person Person {get; set;} 
    public House House {get; set;} 
} 

public class Person 
{ 
    public int Id {get; set;} 
    public string FirstName {get; set;} 
} 

渴望负荷

List<Address> eagerLoadedEntity = Context.Addresses 
         .Include("House") 
         .Include("House.Occupants") 
         .Where(a => a.Address.AddressLine1.StartsWith("a")) 
         .ToList(); 
+0

这样做会导致相同的错误。 – CodeTower

+0

@Kabahango谢谢你让我的谷歌复杂类型...我相信你也许是正确的那里... Thx – Seabizkit

相关问题