每个层次结构的实体框架表错误 3034

发布于 2024-09-14 07:32:14 字数 440 浏览 4 评论 0原文

我有一个使用每个层次结构表的实体框架模型。基类是抽象的,有两个派生类。

我想在这两个派生类和另一个类之间创建关联。这些是多对多关系,因此请通过连接表。

添加第一个关联是可以的,但是当我添加第二个关联时,我收到此错误:

错误 3034:从第 1074 行、第 1082 行开始映射片段时出现问题:具有可能不同键的两个实体被映射到同一行。确保这两个映射片段将 AssociationSet 的两端映射到相应的列。

下面是模型相关部分的图像(LabelImages 和 PresetImages 关联是造成问题的原因):

< img src="https://i.sstatic.net/8zag4.png" alt="替代文字">

I have an Entity Framework model using table per hierarchy. The base class is abstract and there are two derived classes.

I want to create associations between these two derived classes and another class. These are many-to-many relations so go through a joining table.

Adding the first association is ok, but when I add the second i get this error:

Error 3034: Problem in mapping fragments starting at lines 1074, 1082:Two entities with possibly different keys are mapped to the same row. Ensure these two mapping fragments map both ends of the AssociationSet to the corresponding columns.

Heres an image of relevant parts of the model (the LabelImages and PresetImages associations are the ones giving the trouble):

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腹黑女流氓 2024-09-21 07:32:14

有点晚了,但无论如何:

如果您与底层数据库相关,这实际上是不可能的。实体框架将为图像表和产品表之间的关联创建一个连接表,而您实际上需要两个不同的连接表。

我建议对数据库中的继承层次结构使用不同的选项。或者放置图像和产品之间的关系,而不是子类型。您可以在产品类上添加方法来过滤特定类型,一种用于预设图像,一种用于标签图像。或者在上下文类上添加一个方法,以使用 检索产品的特定类型的图像类型

A bit late but anyhow:

This is not really possible if you relate to the underlying database. Entity framework will create one join table for the association between the image table and the product table while you actually need two different ones.

I suggest using either a different option for your inheritance hierarcy in the database. Or put the relation between Image and Product, not the subtypes. You can add methods on the product class to filter on a specific type, one for presetimages one for labelimages. Or add a method on your context class to retrieve a particular type of images for a product using OfType .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文