我有一个名为“地址”的实体。地址包含一个复杂类型,称为 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.
您将“占用者”视为“包含”语句中“房屋”的导航属性,我认为这可能是问题所在。
关于c# - 复杂类型引用的 EagerLoad 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33540286/