未加载可选的导航属性 [英] Optional navigation property is not being loaded

查看:121
本文介绍了未加载可选的导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法终生弄清楚为什么Entity Framework(4.1,代码优先)未实现可选的导航属性.

POCO:

public class PlanMember {
    public Int64 Id { get; set; }

    public String FirstName { get; set; }

    public virtual SystemUser CaseManager { get; set; }

    public String LastName { get; set; }

    public virtual PlanMemberStatus Status { get; set; }
}

public class SystemUser {
    public String EMail { get; set; }

    public String FirstName { get; set; }

    public String Id { get; set; }

    public String LastName { get; set; }
}

public class PlanMemberStatus {
    public Int32 Id { get; set; }

    public String Code { get; set; }
}

配置类:

public class ForPlanMemberEntities:EntityTypeConfiguration<PlanMember> {
    public ForPlanMemberEntities(String schemaName) {
        this.HasOptional(e => e.CaseManager)
            .WithMany()
            .Map(m => m.MapKey("CaseManagerID"));
        this.HasRequired(e => e.Status)
            .WithMany()
            .Map(m => m.MapKey("StatusID"));
        this.ToTable("PlanMember", schemaName);
    }
}

public class ForSystemUserEntities:EntityTypeConfiguration<SystemUser> {
    public ForSystemUserEntities(String schemaName) {
        this.ToTable("SystemUser", schemaName);
    }
}

public class ForPlanMemberStatusEntities:EntityTypeConfiguration<PlanMemberStatus> {
    public ForPlanMemberStatusEntities(String schemaName) {
        this.ToTable("PlanMemberStatus", schemaName);
    }
}

上下文:

public class Context:DbContext {
    public DbSet<PlanMember> PlanMemberDbSet { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        base.OnModelCreating(modelBuilder);

        var schemaName = Properties.Settings.Default.SchemaName;
        modelBuilder.Configurations
            .Add(new Configuration.ForPlanMemberEntities(schemaName))
            .Add(new Configuration.ForPlanMemberStatusEntities(schemaName))
            .Add(new Configuration.ForSystemUserEntities(schemaName))
            ;
    }
}

基于该配置,如果我运行如下查询,则不会填充CaseManager属性. PlanMemberStatus属性可以很好地加载.

var test = (from member in PlanMemberDbSet
            where member.CaseManager != null
            select member).First();
var cm = test.CaseManager;

在我的示例中,cm始终为null,尽管从逻辑上讲应该是不可能的.有什么想法吗?

编辑-如果我执行PlanMemberDbSet.Include("CaseManager"),那么它将按预期工作.为什么这是必要的?在其他情况下,我不必这样做.

我的猜测是PlanMember表中的CaseManagerID外键列值不同于表Id主键列值>-对于.NET有所不同,但对于SQL Server(具有标准排序系统)而言则没有相同,例如,大小写不同:CaseManagerID中为" a ",而在大小写中为" A " Id.

查询!= null(在SQL中转换为IS NOT NULL)是可行的,因此,实际上,您仅获得在数据库中分配了SystemUserPlanMember实体.同样,在数据库(INNER JOIN)中正确执行CaseManager的查询(通过延迟加载触发),并将其传输到客户端.即使对象实现也起作用(上下文中有2个对象).但是EF无法将已加载的PlanMember与已加载的CaseManager相关联,因为密钥不相同(例如,关于大写/小写字母).结果,您的导航属性为null.

Status导航属性中不会发生这种情况,因为键是Int32.

问题似乎与此密切相关:

I can't for the life of me figure out why Entity Framework (4.1, Code First) is not materializing an optional navigation property.

POCOs:

public class PlanMember {
    public Int64 Id { get; set; }

    public String FirstName { get; set; }

    public virtual SystemUser CaseManager { get; set; }

    public String LastName { get; set; }

    public virtual PlanMemberStatus Status { get; set; }
}

public class SystemUser {
    public String EMail { get; set; }

    public String FirstName { get; set; }

    public String Id { get; set; }

    public String LastName { get; set; }
}

public class PlanMemberStatus {
    public Int32 Id { get; set; }

    public String Code { get; set; }
}

Configuration Classes:

public class ForPlanMemberEntities:EntityTypeConfiguration<PlanMember> {
    public ForPlanMemberEntities(String schemaName) {
        this.HasOptional(e => e.CaseManager)
            .WithMany()
            .Map(m => m.MapKey("CaseManagerID"));
        this.HasRequired(e => e.Status)
            .WithMany()
            .Map(m => m.MapKey("StatusID"));
        this.ToTable("PlanMember", schemaName);
    }
}

public class ForSystemUserEntities:EntityTypeConfiguration<SystemUser> {
    public ForSystemUserEntities(String schemaName) {
        this.ToTable("SystemUser", schemaName);
    }
}

public class ForPlanMemberStatusEntities:EntityTypeConfiguration<PlanMemberStatus> {
    public ForPlanMemberStatusEntities(String schemaName) {
        this.ToTable("PlanMemberStatus", schemaName);
    }
}

Context:

public class Context:DbContext {
    public DbSet<PlanMember> PlanMemberDbSet { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        base.OnModelCreating(modelBuilder);

        var schemaName = Properties.Settings.Default.SchemaName;
        modelBuilder.Configurations
            .Add(new Configuration.ForPlanMemberEntities(schemaName))
            .Add(new Configuration.ForPlanMemberStatusEntities(schemaName))
            .Add(new Configuration.ForSystemUserEntities(schemaName))
            ;
    }
}

Based on that configuration, if I run a query like the following it will not populate the CaseManager property. The PlanMemberStatus property loads just fine.

var test = (from member in PlanMemberDbSet
            where member.CaseManager != null
            select member).First();
var cm = test.CaseManager;

In my example cm is always null, despite the fact that - logically speaking - it should be impossible. Any thoughts?

EDIT - If I do PlanMemberDbSet.Include("CaseManager") then it works as expected. Why is this necessary? In other scenarios I haven't had to do this.

解决方案

My guess is that the CaseManagerID foreign key column value in the PlanMember table is different to the Id primary key column value in table SystemUser - different for .NET but not for SQL Server (with standard sorting system), for example different in casing: "a" in CaseManagerID but "A" in Id.

Querying for != null (translates into IS NOT NULL in SQL) works, so you get indeed only the PlanMember entities which have a SystemUser assigned in the database. Also the query by the CaseManager (triggered by lazy loading) is correctly executed in the database (INNER JOIN) and transmitted to the client. Even the object materialization works (there are 2 objects in the context). But EF fails to relate the loaded PlanMember with the loaded CaseManager because the keys are not identical (for example with respect to capital/small letters). As a result your navigation property is null.

This doesn't occur with the Status navigation property because the key is an Int32.

The problem looks closely related to this one: EF 4.1 Code First bug in Find/Update. Any workaround? Should it be reported?

这篇关于未加载可选的导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆