Polymorphic cross-associations on Entity Framework

依然范特西╮ 提交于 2020-01-14 11:07:38

问题


OK, this is an interesting and most importably real urgent problem for me to solve... In order for others to neatly comprehend it, I've stretched myself to make a well illustrated post.


The Object Model

So I have this simple, easy and "beautiful" model in mind. See the first picture. (You can ignore PathEntry, it's not relevant in my situation.)

The idea is that a MediaFeedItem owns:

  • a collection of ThumbnailFileEntries (accesible through the ThumbnailFiles property)
  • at most 1 raw FileEntry (MetadataFile property) and
  • at most 1 MediaFileEntry (MediaFile property)

We shall refer to these last three entity types as the file entities.

Now there's more: As you can see, I am inheriting both ThumbnailFileEntry and MediaFileEntry from FileEntry, and let's not debate that! (for now), it's one of those end-of-story aspects of the design and both entity types will continue to grow later on.

This already brings me some significant issues right away in regards to the polymorphic associations induced by the relationships from the file entities to MediaFeedItem.

The first thing that you shall observe is that I have eliminated the navigation property from the derived file entities (ThumbnailFileEntry and MediaFileEntry) to the primary entity MediaFeedItem.

I do this because they already inherit that property defined in the base class FileEntry. As you can see, I do not delete the roles at the end of these associations.


The Relational Model

I shall be using the so-vastly-conceptually-superior TPT strategy for generating and mapping my Object Model to the RDB world (vs TPH/TPC).

I'm using EF5-rc, the EDMX model designer to design my model, and the EF5 DbContext Generator to generate a DbContext and POCOs cuz I wanna use the DbContext API.

As you can see, I can nicely generate the database model using the EF tools:


The Problem

When loading a new MediaFeedItem and saving it, I get the following error:

System.InvalidOperationException: Multicplicity constraint violated. The role 'MetadataFile' of the relationship 'MediaFeedModel.MediaFeedItem_MetadataFile' has multiplicity 1 or 0..1.

What am I doing wrong?


回答1:


Looking at your problem one thing stands out, The FK relationship between File and MediaFeedItem is required (IE a file must have a MediaFeedItem), but in the case where you are in an extended version of File you probably dont want this.

What i think you want to do is one of the following:

  • change the multiplicity on MediaFeedItem_FileEntry to 0..1 - 0..1 so that it isnt required at either end
  • create a new extended type to handle your metadataFile type and remove the direct reference between the base type and MediaFeedItem

I personally think the second is a more elegant solution to your problem as its creating an actual type for your MetadataFile

What appears to be happening is that you are trying to create an extended type but the base type isnt actually a metadata file.



来源:https://stackoverflow.com/questions/11270620/polymorphic-cross-associations-on-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!