Context:
I am trying to use the same set of models for both XML deserialization and EF 4.1 persistance of data. I cannot change either the existing XSD or the database schema.
Problem:
The XML structure doesn’t line up with the table structure very well for a few models. Currently a database one-to-many relationship is defined in the (XML-based) models as a three level hierarchy of parent-child-child. This causes the error:
The expression ‘t => t.PhysicalDetails.PhysicalFeatures’
is not a valid property expression.
Participant
class Participant {
public PhysicalDetailsType PhysicalDetails { get; set; }
}
PhysicalDetailsType
class PhysicalDetailsType {
[XmlArray("PersonPhysicalFeature")]
public List<PhysicalFeatureType> PhysicalFeatures { get; set; }
}
PhysicalFeatureType
class PhysicalFeatureType {
public int CaseSk { get; set; }
public int ParticipantSk { get; set; }
public Participant participant { get; set; }
}
PhysicalFeatureType EF Mapping
class PhysicalFeatureMap : EntityTypeConfiguration<PhysicalFeatureType> {
HasRequired(t => t.Participant)
.WithMany(t => t.PhysicalDetails.PhysicalFeatures)
.HasForeignKey(d => new { d.CaseSk, d.ParticipantSk});
}
All I’ve come up with so far is creating a proxy property that just hides the nesting:
Participant
Seems to be working so far.