指向成员内成员的 EF 属性

发布于 2024-11-26 16:24:04 字数 1185 浏览 1 评论 0原文

上下文: 我尝试对 XML 反序列化和 EF 4.1 数据持久性使用同一组模型。 我无法更改现有的 XSD 或数据库架构。

问题: 对于某些模型,XML 结构与表结构不太相符。目前,数据库一对多关系在(基于 XML 的)模型中被定义为父子子的三级层次结构。这会导致错误:

表达式 't => t.PhysicalDetails.PhysicalFeatures' 不是有效的属性表达式。

参与者

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 映射

class PhysicalFeatureMap : EntityTypeConfiguration<PhysicalFeatureType> {
  HasRequired(t => t.Participant)
    .WithMany(t => t.PhysicalDetails.PhysicalFeatures)
    .HasForeignKey(d => new { d.CaseSk, d.ParticipantSk});
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空气里的味道 2024-12-03 16:24:04

到目前为止,我所想出的只是创建一个仅隐藏嵌套的代理属性:

Participant

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

到目前为止似乎正在工作。

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.

↙温凉少女 2024-12-03 16:24:04

所有映射方法(HasRequiredWithMany 等)中的表达式必须指定在方法的泛型类型上声明的属性(在您的示例中,lambda 变量 t 的类型为 Participant)。 Participant 没有属性 PhysicalDetails.PhysicalFeatures。也就是说,该表达式不能包含多个点。

如果我理解正确的话,数据库中只有两个表(对于ParticipantPhysicalFeatureType),但是三个类在您的模型中(中间有附加的 PhysicalDetailsType)。 PhysicalDetailsType 是一种“中间”类型,对于模型中的一对多关系是必需的,但不作为数据库中的表存在,对吧? (只是为了理解,并不是说我知道如何映射它或者是否有可能。)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文