Association inheritance with Entity Framework

Consider, for example, my next database model.

  • UserTable (Id, Username)
  • TextContentTable (Id, Name, Description, CreatedByUserId, ModifiedByUserId, TextContent)
  • ImageContentTable (Id, Name, Description, CreatedByUserId, ModifiedByUserId, ImageContent)

I would like to create the following object model. But is it possible? For instance:

  • BaseEntity (Id),
  • DescriptiveEntity (Name, description): BaseEntity
  • TrackedEntity ( CreatedBy , ModifiedBy ): DescriptiveEntity
  • User (username): BaseEntity
  • TextContent (Text): TrackedEntity
  • ImageContent (Image): TrackedEntity

I hope you understood my notation, but is it possible to map associations (in bold) to separate TextContentTable and ImageContentTable?

Thanx in advance.

0


a source to share


1 answer


I'm not sure if this approach is a good idea with the current Entity Framework entity version. When a type such as TextContent inherits from a type of type BaseEntity, it must be the same object as the base type. With the strategy outlined in the question, all objects in your application will have the same set of objects. I'm not sure what you want.

However, you can get the same effect through interfaces. For example, you might have an IEntity interface that provides an id property. Another interface, ITRAckedEntity can expose CreatedBy and ModifiedBy. Etc.



It's not ideal, but it's more than enough for us. We actually use codegen with T4 templates to implement them.

The next version of the Entity Framework may have features that make it easier to implement this inheritance pattern without having to use a single set of objects.

+1


a source







All Articles