4

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.

EDMX Model Diagram

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.

MediaFeedItem_MediaFile association

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.

MediaFeedItem_MetadataFile association


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:

DB Model Diagram


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?

4

1 回答 1

2

看看你的问题,有一件事很突出,File 和 MediaFeedItem 之间的 FK 关系是必需的(即文件必须有一个 MediaFeedItem),但如果你在 File 的扩展版本中,你可能不想要这个。

我认为你想要做的是以下之一:

  • 将 MediaFeedItem_FileEntry 上的多重性更改为 0..1 - 0..1 以便在任一端都不需要
  • 创建一个新的扩展类型来处理您的 metadataFile 类型并删除基类型和 MediaFeedItem 之间的直接引用

我个人认为第二个是解决您的问题的更优雅的解决方案,因为它为您的 MetadataFile 创建了一个实际类型

似乎正在发生的事情是您正在尝试创建扩展类型,但基本类型实际上并不是元数据文件。

于 2012-06-30T02:46:40.307 回答