指向成员内成员的 EF 属性
上下文: 我尝试对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
到目前为止,我所想出的只是创建一个仅隐藏嵌套的代理属性:
Participant
到目前为止似乎正在工作。
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.
所有映射方法(
HasRequired
、WithMany
等)中的表达式必须指定在方法的泛型类型上声明的属性(在您的示例中,lambda 变量t
的类型为Participant
)。Participant
没有属性PhysicalDetails.PhysicalFeatures
。也就是说,该表达式不能包含多个点。如果我理解正确的话,数据库中只有两个表(对于
Participant
和PhysicalFeatureType
),但是三个类在您的模型中(中间有附加的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 variablet
which is of typeParticipant
in your example).Participant
doesn't have a propertyPhysicalDetails.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
andPhysicalFeatureType
) but three classes in your model (with the additionalPhysicalDetailsType
in between). ThePhysicalDetailsType
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.)