简体   繁体   English

在Entity Framework中以多对多关系映射复合外键

[英]Mapping composite foreign keys in a many-many relationship in Entity Framework

I have a Page table and a View table. 我有一个Page表和一个View表。 There is a many-many relationship between these two via a PageView table. 这两者之间通过PageView表有很多关系。 Unfortunately all of these tables need to have composite keys (for business reasons). 不幸的是,所有这些表都需要具有复合键(出于商业原因)。

  • Page has a primary key of (PageCode, Version), 页面有一个主键(PageCode,Version),
  • View has a primary key of (ViewCode, Version). View具有(ViewCode,Version)的主键。
  • PageView obviously enough has PageCode, ViewCode, and Version. PageView显然足够有PageCode,ViewCode和Version。
  • The FK to Page is (PageCode, Version) and the FK to View is (ViewCode, Version) FK to Page是(PageCode,Version)和FK to View是(ViewCode,Version)

Makes sense and works, but when I try to map this in Entity framework I get 有意义并且有效,但当我尝试在Entity框架中映射时,我得到了

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) 错误3021:映射片段中的问题...:表PageView中的以下每个列都映射到多个概念侧属性:PageView.Version映射到(PageView_Association.View.Version,PageView_Association.Page.Version)

So clearly enough, EF is having a complain about the Version column being a common component of the two foreign keys. 很明显,EF抱怨版本列是两个外键的常见组件。

Obviously I could create a PageVersion and ViewVersion column in the join table, but that kind of defeats the point of the constraint, ie the Page and View must have the same Version value. 显然,我可以在连接表中创建一个PageVersion和ViewVersion列,但这种方法会破坏约束点,即Page和View必须具有相同的Version值。

Has anyone encountered this, and is there anything I can do get around it? 有没有人遇到这个,有什么我可以做到的吗? Thanks! 谢谢!

I'm not aware of a solution in Entity Framework for this problem, but a workaround could be to add primary key columns to your tables and add a unique constraints on the fields you wanted to act like a composite key. 我不知道实体框架中针对此问题的解决方案,但解决方法可能是将主键列添加到表中,并在要作为复合键的字段上添加唯一约束。 This way you ensure uniqueness of your data, but still have one primary key column. 这样,您可以确保数据的唯一性,但仍然有一个主键列。 Pro-con arguments can be found under this topic: stackoverflow question 在本主题下可以找到Pro-con参数: stackoverflow问题

Cheers 干杯

经过多次阅读和讨论后,这只是EF设计师和验证员在处理多种关系时的一个限制。

I was going to write that you should use a surrogate key, but I don't think this will actually help you. 我打算写一下你应该使用代理键,但我认为这不会对你有所帮助。 The join table is enforcing a business rule basedon the logical attributes of the entities - these same attributes would be stored in the join table even if Page and View were augmented with surrogate keys. 连接表基于实体的逻辑属性强制执行业务规则 - 即使使用代理键扩充了页面和视图,这些相同的属性也将存储在连接表中。

If you are executing on a server that supports constraints, you could separate the Version into PageVersion and ViewVersion and add a constraint that the two are equal, or use an INSERT/UPDATE trigger to enforce this. 如果在支持约束的服务器上执行,则可以将Version分隔为PageVersion和ViewVersion,并添加两者相等的约束,或使用INSERT / UPDATE触发器强制执行此操作。

I may have simply misunderstood the intent, but I feel there is something that doesn't seem right with this design. 我可能只是误解了意图,但我觉得这个设计似乎有些不对劲。 I can't imagine how the versioning will work as pages and views are changed and new versions created. 我无法想象当页面和视图被更改以及创建新版本时版本控制将如何工作。 If changing a page means it gets a new version, then it will also have cause new versions of all it's views to be made, even for views that haven't changed in that version. 如果更改页面意味着它获得了新版本,那么它也会导致所有视图的新版本,即使对于那个版本中没有更改的视图也是如此。 Equivalently, if one view in a page changes, the view's version changes, which means the page's version must also change, and so all other views in that page, since page and view versions must match. 同样,如果页面中的一个视图发生更改,则视图的版本也会更改,这意味着页面的版本也必须更改,因此该页面中的所有其他视图都会更改,因为页面和视图版本必须匹配。 Does this seem right? 这看起来是对的吗?

Consider using nHibernate? 考虑使用nHibernate? :) - or at least for anything more than simple joins in your DB. :) - 或者至少比数据库中的简单连接更多。 Im working with EF4 and it doesnt seem mature enough for complex data graphs IMO, at the moment. 我正在使用EF4,目前它似乎还不够复杂的数据图表IMO。 Hopefully it will get there though! 希望它会到达那里!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM