La propiedad de navegación opcional no se está cargando

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

Antecedentes:

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 propiedad. los PlanMemberStatus property loads just fine.

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

En mi ejemplo cm es siempre null, despite the fact that - logically speaking - it should be impossible. Any thoughts?

EDITAR - 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.

preguntado el 08 de noviembre de 11 a las 17:11

1 Respuestas

Mi conjetura es que el 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"En CaseManagerID pero "A"En Id.

Consultando por != 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: Error del primer código de EF 4.1 en Buscar / Actualizar. ¿Alguna solución? ¿Debería informarse?

contestado el 23 de mayo de 17 a las 15:05

Your first paragraph is 100% correct. The PlanMember table had values all in upper-case, where the SystemUser table had values all in lower-case. Once I made them both the same case it magically works. Absolutely a bug, IMO. - ¡Qué asco!

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.