在实体框架多对多的映射关系的复合外键 [英] Mapping composite foreign keys in a many-many relationship in Entity Framework

查看:178
本文介绍了在实体框架多对多的映射关系的复合外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页表和视图表。有通过网页浏览表这两个之间的多对多关系。不幸的是所有这些表需要有(出于商业原因)组合键。

I have a Page table and a View table. There is a many-many relationship between these two via a PageView table. Unfortunately all of these tables need to have composite keys (for business reasons).


  • 页有(PageCode版本)的一个主键,

  • 查看有(ViewCode,版)的主键。

  • 浏览量显然不够有PageCode,ViewCode和版本。

  • 的FK页是(PageCode,版)和FK查看是(ViewCode,版本)

  • Page has a primary key of (PageCode, Version),
  • View has a primary key of (ViewCode, Version).
  • PageView obviously enough has PageCode, ViewCode, and Version.
  • The FK to Page is (PageCode, Version) and the FK to View is (ViewCode, Version)

有道理和作品,但是当我尝试在实体框架图此,我得到

Makes sense and works, but when I try to map this in Entity framework I get

错误3021:问题映射
片段...:每个表浏览量以下
的列被映射到
多个概念端属性:
PageView.Version被映射到
(PageView_Association。 View.Version,
PageView_Association.Page.Version)

Error 3021: Problem in mapping fragments...: Each of the following columns in table PageView is mapped to multiple conceptual side properties: PageView.Version is mapped to (PageView_Association.View.Version, PageView_Association.Page.Version)

所以很明显的是,EF是有一个抱怨的版本列作为两个外键的共同组成部分。

So clearly enough, EF is having a complain about the Version column being a common component of the two foreign keys.

很显然,我可以创建连接表中的一个PageVersion和ViewVersion列,但那种失败的约束点,即页面和视图必须具有相同的版本价值。

Obviously I could create a PageVersion and ViewVersion column in the join table, but that kind of defeats the point of the constraint, i.e. the Page and View must have the same Version value.

有没有人遇到过这一点,有什么我能做得到解决呢?谢谢!

Has anyone encountered this, and is there anything I can do get around it? Thanks!

推荐答案

在大量的阅读和搞乱,这仅仅是一个多对一工作时,EF设计的限制和验证许多关系。

After much reading and messing about, this is just a limitation of the EF designer and validator when working with many-many relationships.

这篇关于在实体框架多对多的映射关系的复合外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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