在实体框架多态的跨协会 [英] Polymorphic cross-associations on Entity Framework

查看:204
本文介绍了在实体框架多态的跨协会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定,这是一个有趣和最importably 真正急需解决的问题作为我解决......为了让其他人整齐地融为一体prehend它,我伸出自己做一个很好的说明后。

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.

所以我有这个简单,容易和美丽模式的初衷。请参见第一张照片。 (您可以忽略 PathEntry ,它不是有关我的情况。)

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.)

我们的想法是, MediaFeedItem 旗下拥有:

The idea is that a MediaFeedItem owns:

  • ThumbnailFileEntries 的集合(入店通过的 ThumbnailFiles 的属性)
  • 在最多1 FileEntry MetadataFile 的属性)和
  • 在最多1 MediaFileEntry 媒体文件的属性)
  • 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.

现在有更多的:正如你所看到的,我继承这两个 ThumbnailFileEntry MediaFileEntry FileEntry 并让我们不要争论了!(现在),它是结束的故事设计的那些方面之一,这两个实体类型将继续增长稍后。

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.

这已经给我带来了一些显著的问题就在问候诱导关系从的文件实体 MediaFeedItem 的多态关联。

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.

这是你必须遵守的第一件事是,我从来源 文件实体的( ThumbnailFileEntry MediaFileEntry )的主要实体 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.

我这样做是因为他们已经继承了基类中定义的属性 FileEntry 。正如你所看到的,我没有在这些协会的末尾删除角色。

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.

我会用所谓的大大概念上的高级TPT策略生成和映射我的对象模型到RDB世界(VS TPH / TPC)。

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).

我使用EF5-RC的EDMX模型设计师设计我的模型,并EF5的DbContext生成器生成的DbContext和波苏斯因为我想使用的DbContext API。

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.

正如你所看到的,我可以很好地使用EF工具生成的数据库模型:

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

在装入新MediaFeedItem并保存它,我得到以下错误:

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

System.InvalidOperationException:Multicplicity约束侵犯。角色MetadataFile的关系,MediaFeedModel.MediaFeedItem_MetadataFile具有多重1或0..1。

我是什么做错了吗?

推荐答案

看你的问题,有一点突出,需要的文件和MediaFeedItem之间的FK关系(即文件必须有一个MediaFeedItem),但在情况下,你是在文件的扩展版本,你可能不希望这样。

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:

  • 更改MediaFeedItem_FileEntry的多重性0..1 - 因此它不是必需的两端0..1
  • 创建一个新的扩展类型来处理你的metadataFile类型和删除的基本类型和MediaFeedItem
  • 之间的直接参考

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

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.

这篇关于在实体框架多态的跨协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆