简体   繁体   English

实体框架的关联继承

[英]Association inheritance with Entity Framework

Consider for instance my following database model. 考虑一下我下面的数据库模型。

  • UserTable(Id, Username) UserTable(ID,用户名)
  • TextContentTable(Id, Name, Description, CreatedByUserId, ModifiedByUserId, TextContent) TextContentTable(Id,名称,描述,CreatedByUserId,ModifiedByUserId,TextContent)
  • ImageContentTable(Id, Name, Description, CreatedByUserId, ModifiedByUserId, ImageContent) ImageContentTable(Id,名称,描述,CreatedByUserId,ModifiedByUserId,ImageContent)

I would like to create the following object model. 我想创建以下对象模型。 But is it even possible? 但是有可能吗? For instance: 例如:

  • BaseEntity(Id) BaseEntity(Id)
  • DescriptiveEntity(Name, Description) : BaseEntity DescriptiveEntity(名称,描述):BaseEntity
  • TrackedEntity( CreatedBy , ModifiedBy ) : DescriptiveEntity TrackedEntityCreatedByModifiedBy ):描述性实体
  • User(Username) : BaseEntity 用户(用户名):BaseEntity
  • TextContent(Text) : TrackedEntity TextContent(Text):TrackedEntity
  • ImageContent(Image) : TrackedEntity ImageContent(Image):TrackedEntity

I hope you understand my notation, but is it possible to map associations (marked in bold) to the individual tables TextContentTable & ImageContentTable? 我希望您理解我的表示法,但是可以将关联(以粗体标记)映射到单个表TextContentTable和ImageContentTable吗?

Thanx in advance. 提前感谢。

I'm not sure this approach is such a good idea with the current, shipping version of the Entity Framework. 我不确定这种方法是否适用于当前发布的Entity Framework版本。 When a type, like TextContent inherits from a type like BaseEntity, it must have the same entity set as the base type. 当诸如TextContent之类的类型继承自诸如BaseEntity的类型时,它必须具有与基本类型相同的实体集。 With the strategy outlined in the question, all entities in your application would have the same entity set. 使用问题中概述的策略,应用程序中的所有实体都将具有相同的实体集。 I'm not sure that's what you want. 我不确定那不是您想要的。

However, you can get much the same effect via interfaces. 但是,您可以通过接口获得几乎相同的效果。 For example, you could have an interface, IEntity which exposes an id property. 例如,您可能有一个接口IEntity,它公开了id属性。 Another interface, ITrackedEntity could expose CreatedBy and ModifiedBy. 另一个接口ITrackedEntity可以公开CreatedBy和ModifiedBy。 Etc. 等等。

This is not ideal, but it's more than good enough for us at present. 这并不理想,但对我们目前而言已经足够了。 We actually use codegen via T4 templates to implement these. 我们实际上是通过T4模板使用codegen来实现这些。

The next version of the Entity Framework may have features which make this inheritance pattern easier to implement without requiring a single entity set. 实体框架的下一个版本可能具有一些特性,这些特性使该继承模式更易于实现,而无需单个实体集。

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

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