简体   繁体   English

实体框架5.0和继承

[英]Entity Framework 5.0 and Inheritance

I have a couple of classes setup like so: 我有几个这样的类设置:

Member (abstract) 成员 (摘要)

  • Property1 Property1
  • Property2 (etc.) Property2(等)

OrganizationMember : Member 组织成员:成员

  • Property3 Property3
  • Property4 (etc.) Property4(等)

PrivateMember : Member 私人会员:会员

  • Property5 Property5
  • Property6 (etc.) Property6(等)

I then have three database tables structured the same way - data common to both types of member entities is stored in the Member table, data specific to OrganizationMember is stored in a corresponding table and data specific to PrivateMember is stored in a corresponding table. 然后,我以相同的方式构造了三个数据库表-两种类型的成员实体共有的数据存储在Member表中,OrganizationMember特定的数据存储在对应的表中,而PrivateMember特定的数据存储在对应的表中。 The Id for the Member table is the same as the Id for the other two tables since it's a 1-1 relationship - just a classification where each type has stuff specific to it. 成员表的ID与其他两个表的ID相同,因为它是1-1的关系-只是一种分类,其中每种类型都有特定于其的内容。 It probably doesn't matter here, but nothing can be a Member directly - they have to be one of the two types. 在这里可能并不重要,但是没有什么可以直接成为成员-它们必须是两种类型之一。

When I setup my mapping (using Fluid - everything with EF is handcoded, no designers and no EDMX files), if I tell OrganizationMember to ignore properties inherited from Member, I get the "You cannot use Ignore method on the property '[PropertyName]' on type '[TypeName]' because this type inherits from the type '[BaseType]' where this property is mapped. To exclude this property from your model, use NotMappedAttribute or Ignore method on the base type." 设置映射时(使用Fluid-带有EF的所有内容都是手工编码的,没有设计器且没有EDMX文件),如果我告诉OrganizationMember忽略从Member继承的属性,则会收到“您无法在属性'[PropertyName]上使用忽略方法'在类型'[TypeName]'上,因为此类型继承自映射此属性的类型'[BaseType]'。要从模型中排除此属性,请对基本类型使用NotMappedAttribute或Ignore方法。” error. 错误。 But when I tell it not to ignore those properties inherited from Member it throws an error ("Problem in mapping fragments starting at [...] [TypeName] are being mapped to the same rows in table Member. Mapping conditions can be used to distinguish the rows that these types are mapped to."). 但是,当我告诉它不要忽略从Member继承的那些属性时,它将引发错误(“映射片段中以[...] [TypeName]开头的问题已被映射到表Member中的相同行。映射条件可用于区分这些类型映射到的行。”)。 The really frustrating part here is the properties erroring out aren't supposed to be mapped at all for either entity - they are part of my overall framework design, but have no database relevancy at all and don't exist there. 这里真正令人沮丧的部分是错误提示的属性根本不应该映射到任何一个实体-它们是我总体框架设计的一部分,但是根本没有数据库相关性,也不存在任何相关性。

I would've assumed this was a pretty standard thing to setup, but EF is really not playing nice. 我本来以为这是一个很标准的设置,但是EF的表现确实不好。 Any suggestions? 有什么建议么?

This article describes the mapping of inheritance types. 本文介绍了继承类型的映射。 It lists different ways to handle mapping to inheritance depending on what you want to achieve. 根据您要实现的目标,它列出了处理映射到继承的不同方法。

http://msdn.microsoft.com/en-us/data/jj591617.aspx#2.5 http://msdn.microsoft.com/en-us/data/jj591617.aspx#2.5

Three different approaches to represent an inheritance hierarchy in Code-First are available: Table Per Hierarchy, Table Per Type, and Table Per Concrete class. 可以使用三种不同的方法在Code-First中表示继承层次结构:表每个层次结构,每个类型的表和每个具体类的表。 Here is details. 这是细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM