views:

390

answers:

1

Consider for instance my following 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 even 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 understand my notation, but is it possible to map associations (marked in bold) to the individual tables TextContentTable & ImageContentTable?

Thanx in advance.

+1  A: 

I'm not sure this approach is such a good idea with the current, shipping version of the Entity Framework. When a type, like TextContent inherits from a type like BaseEntity, it must have the same entity set as the base type. 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. Another interface, ITrackedEntity could expose CreatedBy and 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.

The next version of the Entity Framework may have features which make this inheritance pattern easier to implement without requiring a single entity set.

Craig Stuntz
First off all thanx for your anwser. Ok I'll understand that this isn't possible in the current shipping version, but I'll hope MS would think about these kind of object models. I will try to do my thing with interfaces, it's far from ideal but I think I can live with it right now ;-) You mention the T4 templates to implement these interfaces but could explain that please.
JSC
The T4 thing is simple. The interface support is identical for every class. So rather than cut and paste it, we have an array of class names and we use T4 to generate code for every class in the array. It generates a partial class which implements the interface. In the next version of EF we'll be able to customize the codegen that the EF itself uses, again via T4.
Craig Stuntz