实体框架的多态交叉关联 [英] Polymorphic cross-associations on Entity Framework

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

问题描述

好的,这是一个有趣的,最重要的真正的紧急问题,我要解决...为了让别人整齐地理解它,我已经伸张自己做一个很好的说明文章。






对象模型



所以我有这个简单,容易和美丽的模式。看第一张照片。 (您可以忽略 PathEntry ,这与我的情况无关。)



这个想法是一个 MediaFeedItem 拥有:




  • 的集合ThumbnailFileEntries (通过缩略图文件属性访问)

  • 最多1 raw FileEntry (MetadataFile 属性)和

  • 最多1 MediaFileEntry MediaFile 属性)



我们将这些最后三个实体类型称为文件实体。 >

现在还有更多:如您所见,我继承了 ThumbnailFileEntry MediaFileEntry FileEntry ,让我们不要争论!(现在),它是其中一个最终的故事设计和两个实体类型将在以后继续增长。





这已经给我带来了关于由文件实体 MediaFeedItem



您要观察的第一件事是,我已经从派生的消除了导航属性 ThumbnailFileEntry MediaFileEntry )到主实体 MediaFeedItem





我这样做是因为他们已经继承了基类 FileEntry 中定义的属性。如您所见,我不删除这些关联结尾的角色。





我将使用如此广泛的概念上优势的TPT策略来生成和映射我的对象模型到RDB世界(vs TPH / TPC)。



我正在使用EF5-rc,EDMX模型设计者设计我的模型,而EF5 DbContext Generator生成一个DbContext和POCOs,我想使用DbContext API。



您可以看到,我可以使用EF工具很好地生成数据库模型:








问题



当加载新的MediaFeedItem并保存它时,我收到以下错误:



System.InvalidOperationException:多重约束中提琴特德。关系MediaFeedModel.MediaFeedItem_MetadataFile的角色MetadataFile具有多重性1或0..1。



我做错了什么? >

解决方案

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



我想你想做的是以下之一:




  • 将MediaFeedItem_FileEntry的多重性更改为0..1 - 0..1,以便在任一端不需要

  • 创建一个新的扩展类型来处理您的metadataFile类型并删除基类和MediaFeedItem之间的直接引用



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



出现了什么要发生的是,您正在尝试创建扩展类型,但基本类型实际上不是元数据文件。


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?

解决方案

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.

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

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