C# 加载由复杂类型引用的实体

C# 加载由复杂类型引用的实体,c#,entity-framework,lazy-loading,eager-loading,C#,Entity Framework,Lazy Loading,Eager Loading,我有一个名为Address的实体。地址包含一个复杂类型,称为House。房屋包含对其居住者的引用。居住者是一个实体 public class Address { [key] public int Id { get; set; } public House House { get; set; } } 众议院: [ComplexType] public class House { [Required] public string HouseType

我有一个名为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; }
}
如果我使用lazyloading,一切正常,我可以访问所有属性。然而,我需要使用预加载,因为在处理上下文很久之后需要实体

我尝试使用以下代码包含属性:

   // DbSet is of type DbSet<Address>
   List<Address> eagerLoadedEntity = DbSet.Where(a => a.Address.StartsWith("a"))
                .Include(a => a.House.Occupants).ToList();
//DbSet属于DbSet类型
List LoadedEntity=DbSet.Where(a=>a.Address.StartsWith(“a”))
.Include(a=>a.House.includes).ToList();
我得到以下错误:

指定的包含路径无效。EntityType“Address”不存在 不声明名为“House”的导航属性


也许根本不可能?

复杂类型不能参与关联,也不能包含导航属性

您在“包含”声明中将“居住者”视为“房屋”上的导航属性,我想这可能就是问题所在。

这是可能的

检查

类似于:这只是一个未经测试的示例。。。你需要调整一下

   List<Address> eagerLoadedEntity = Context.Addresses
                         .Include("House")
                         .Include("House.Occupants")
                         .Where(a => a.Address.StartsWith("a"))
                         .ToList();
List-loadedentity=Context.Addresses
.包括(“房屋”)
.包括(“房屋、住户”)
.Where(a=>a.Address.StartsWith(“a”))
.ToList();
更新

对不起,我想你可能对复杂类型的看法是正确的。。。。但如果它们都是db实体,那么您应该能够。。。类似于。。。仅供参考

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;}
}
公共类地址
{
公共int Id{get;set;}
public int HouseId{get;set;}
公共字符串AddressLine1{get;set;}
酒店{get;set;}
}
公屋
{
公共int Id{get;set;}
公共字符串HouseType{get;set;}
公共虚拟ICollection{get;set;}
}
公共舱乘客
{
公共int Id{get;set;}
public int HouseId{get;set;}
公共int PersonId{get;set;}
公共bool IsOwner{get;set;}
公共日期时间起始日期{get;set;}
公共日期时间结束日期{get;set;}
公众人物{get;set;}
酒店{get;set;}
}
公共阶层人士
{
公共int Id{get;set;}
公共字符串名{get;set;}
}
急件

List<Address> eagerLoadedEntity = Context.Addresses
                         .Include("House")
                         .Include("House.Occupants")
                         .Where(a => a.Address.AddressLine1.StartsWith("a"))
                         .ToList();
List-loadedentity=Context.Addresses
.包括(“房屋”)
.包括(“房屋、住户”)
.Where(a=>a.Address.AddressLine1.StartsWith(“a”))
.ToList();

DbSet是什么类型的<代码>数据库集?哦。。。。等待您在Address类中有一个Address类。。。。也许你想在那里建房子?@AnestisKivranoglou是的,我想在那里建房子。等等,你的数据库实际上是什么样子的?您是如何创建该数据库的?它工作的原因是从占用者到地址的反向引用。对于本例中所示的情况,这不是一个理想的方案,但是在上下文中它是有意义的。我做了一个变通方法,使用额外的查询获取相关实体。这样做将导致相同的错误。@Kabahango感谢您让我使用google ComplexType。。。我相信你可能是对的。。。谢谢
List<Address> eagerLoadedEntity = Context.Addresses
                         .Include("House")
                         .Include("House.Occupants")
                         .Where(a => a.Address.AddressLine1.StartsWith("a"))
                         .ToList();