问题
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.
回答1:
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?
来源:https://stackoverflow.com/questions/8054577/optional-navigation-property-is-not-being-loaded