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
CaseManagerIDforeign key column value in thePlanMembertable is different to theIdprimary key column value in tableSystemUser– different for .NET but not for SQL Server (with standard sorting system), for example different in casing: “a” inCaseManagerIDbut “A” inId.Querying for
!= null(translates intoIS NOT NULLin SQL) works, so you get indeed only thePlanMemberentities which have aSystemUserassigned in the database. Also the query by theCaseManager(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 loadedPlanMemberwith the loadedCaseManagerbecause the keys are not identical (for example with respect to capital/small letters). As a result your navigation property isnull.This doesn’t occur with the
Statusnavigation property because the key is anInt32.The problem looks closely related to this one: EF 4.1 Code First bug in Find/Update. Any workaround? Should it be reported?