复杂类型引用的 EagerLoad 实体

EagerLoad entity referenced by complex type

我有一个名为地址的实体。 Address 包含一个复杂类型,称为 House。房屋包含对其居住者的引用。占用者是一个实体。

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 很久之后还需要实体。

我已尝试使用此代码包含属性:

   // 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'.

也许根本不可能? MSDN Complex Types

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

您在 "Include" 声明中将 "Occupants" 视为 "House" 上的导航 属性,我想这可能是问题所在。

这是可能的

检查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();

更新

抱歉,我认为您可能对 ComplexTypes 是正确的……但如果它们都是数据库实体,那么您应该能够做……类似……仅供参考

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();