简体   繁体   English

在实体框架中使用父子FK

[英]Using Parent and Child FK in Entity Framework

There are some situations using Foreign Key in a table that FK has Parent or Child relations with another table as shown below: 在某些情况下,在表中使用外键时,FK与另一个表具有父关系或子关系,如下所示:

Staff: 员工:

Id   |  Name  |  CityId |
-------------------------
1001 |  John  |  1      |
1002 |  Mary  |  2      |
1003 |  Bill  |  3      |
1004 |  Jose  |  4      |
1005 |  Anna  |  5      |

City: 市:

Id  |  Name   |  CountryId |
----------------------------
1   |  NY     |  101       |
2   |  Paris  |  102       |
3   |  London |  103       |
4   |  Rome   |  104       |
5   |  Tokyo  |  105       |

Country: 国家:

Id  |  Name   | 
---------------
101 |  USA    |
102 |  France |
103 |  UK     |
104 |  Italy  |
105 |  Japan  |

My question is that: Should we use only CityId as FK in the Staff entity, or is it better to include CityId and its parent CountryId in the Staff entity? 我的问题是:我们应该在Staff实体中仅使用CityId作为FK,还是在Staff实体中包含CityId及其父CountryId更好? Of course it seems to be redundant to include a FK and its parent as FK, but I wanted to be clarified if there is some situations or requirements when using such a king of relations with cascade? 当然,将FK及其父级作为FK似乎是多余的,但是我想澄清一下,当使用这种级联关系之王时是否存在某些情况或要求? Which one should be used? 应该使用哪一个?

I would avoid breaking normalization because there is no simple way to enforce that a staff's Country reference and it's City's Country reference are guraranteed to be in sync. 我将避免破坏规范化,因为没有简单的方法可以强制要求员工的Country参考和City的Country参考保持同步。 For instance, a Staff might have a City set to "Toronto" with a Country set to "Canada", but then somewhere the City is updated to "Boston", but Staff.Country is not updated. 例如,某个工作人员可能将“城市”设置为“多伦多”,而“国家/地区”设置为“加拿大”,但是随后某处将城市更新为“波士顿”,但没有更新“工作人员。国家/地区”。 Staff says country is "Canada" while it's City says country is "USA". 工作人员说国家是“加拿大”,而城市则是国家“美国”。 Who is trusted as the source of truth? 谁被信任为真理之源?

When it comes to displaying information about staff, use View Models to flatten out relevant details. 在显示有关员工的信息时,请使用“视图模型”展平相关细节。 If you want to display staff details with a Country name and nothing else about the city, the view model can expose that based on data selected from the entities. 如果要显示带有国家(地区)名称的职员详细信息,而没有关于城市的其他信息,则视图模型可以基于从实体中选择的数据来显示该细节。 For instance: 例如:

var staffViewModels = context.Staff
    .Select(x => new StaffViewModel
    {
        StaffId = x.Id,
        Name = x.Name,
        Country = x.City.Country.Name
    }).ToList();

Even if the structure introduced an Address for staff which consisted of a City which related to a country: 即使结构引入了由与国家相关的城市组成的工作人员地址:

var staffViewModels = context.Staff
    .Select(x => new StaffViewModel
    {
        StaffId = x.Id,
        Name = x.Name,
        Country = x.Address.City.Country.Name
    }).ToList();

The entity can remain normalized and generate the SQL to efficiently access the relevant data. 实体可以保持规范化并生成SQL以有效访问相关数据。

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

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