c# - 复杂类型引用的 EagerLoad 实体

标签 c# entity-framework lazy-loading eager-loading

我有一个名为“地址”的实体。地址包含一个复杂类型,称为 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/

相关文章:

java - hibernate/ Spring : failed to lazily initialize - no session or session was closed

java - 如何使用 SmartGWT 和 SQL 实现惰性列表

c# - 任何非法跨线程操作异常的解决方案?

c# - LIKE 查询中使用的包含 "' "(撇号)的参数的设置值

entity-framework - 使用 Entity Framework 从一个数据库故障转移到另一个数据库的一般模式?

Angular 4.4.6 延迟加载模块中的共享服务

C# 启动一个新的 MailTo 进程和 HTML URL 编码

c# - 域实体中的外键属性

c# - 将 SQL 结果集转换为 CSV 文件

c# - 没有使用()范围的DAL方法中的 Entity Framework 新的dbContext