开发者

EF Property pointing to a member within a member

Context: I am trying to use the same set of models for both XML des开发者_开发技巧erialization 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

class Participant {
  public PhysicalDetailsType PhysicalDetails { get; set; }
  public List<PhysicalFeatureType> PhysicalFeatures {
    get { return PhysicalDetails.PhysicalFeatures; }
    set { Physicaldetails.PhysicalFeatures = value; }
  }
}

Seems to be working so far.


The expression in all the mapping methods (HasRequired, WithMany, etc.) must specify a property which is declared on the generic type of the method (the lambda variable t which is of type Participant in your example). Participant doesn't have a property PhysicalDetails.PhysicalFeatures. So to say, the expression cannot contain more than one dot.

If I understand right, you only have two tables in the DB (for Participant and PhysicalFeatureType) but three classes in your model (with the additional PhysicalDetailsType in between). The PhysicalDetailsType is an "intermediate" type, necessary for the one-to-many relationship in the model but not existing as table in the database, right? (Just to understand, not that I would have an idea how to map this or if it's possible at all.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜