简体   繁体   English

实体框架组合键

[英]Entity Framework Composite Key

I have a database with two tables, customer and account. 我有一个包含两个表的数据库,客户和帐户。 Because multiple customers can exist on multiple accounts this is a many to many design. 因为多个客户可以存在于多个帐户上,所以这是多对多设计。

This is how I designed it in SQL 这就是我在SQL中设计的方式

在此处输入图片说明

This works quite nicely as Entity Framework picks up that its a mapping table and just maps Customer to Account as lists on each and hiding the mapping table. 这很有效,因为Entity Framework选择了它的映射表,然后将Customer映射到Account作为每个列表的列表,并隐藏了映射表。 Brilliant! 辉煌!

I would like to extend this further to add preferences to a mapping between a customer and an account like such: 我想进一步扩展此功能,以将首选项添加到客户与这样的帐户之间的映射中:

在此处输入图片说明

Am I right in assuming this is not possible? 我认为这不可能吗? I have tried adding it to EF model but instead it brings back the mapping table. 我尝试将其添加到EF模型中,但是它带回了映射表。

Anyone else had any luck with this? 其他人有运气吗?

What you have in your first case is a simple many to many relation table, which in Entity Framework results in the collections of entities on one another. 在第一种情况下,您拥有的是一个简单的多对多关系表,该表在Entity Framework中导致彼此之间的实体集合。

If you want to have a relation to the Preference from your CustomerAccount relation table, the relation becomes complex and it cannot be depicted in the simple relation lists anymore. 如果要从CustomerAccount关系表中与Preference建立关系,则该关系会变得很复杂,无法再在简单的关系列表中进行描述。 You need add an entity for your relation CustomerAccount which will have foreign keys on Customer , Account and Preference . 您需要为CustomerAccount添加一个实体,该实体在CustomerAccountPreference上具有外键。

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

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