简体   繁体   English

跨多个列的唯一实体框架

[英]Entity Framework Unique Across Multiple Columns

Given the following table给定下表

Year - Int年份 - 诠释
EmployeeOne - String EmployeeOne - 字符串
EmployeeTwo - String EmployeeTwo - 字符串

How can we prevent duplicate values across different columns.我们如何防止不同列之间的重复值。

Take the following scenario采取以下场景

Year | EmployeeOne | EmployeeTwo
2020 | 0001        | 0002
2020 | 0003        | 0004

This is fine as EmployeeOne and EmployeeTwo don't contain the same values.这很好,因为 EmployeeOne 和 EmployeeTwo 不包含相同的值。

However given this scenario但是鉴于这种情况

Year | EmployeeOne | EmployeeTwo
2020 | 0001        | 0002
2020 | 0003        | 0001

I want this to fail.我希望这失败。 As EmployeeOne column has 0001 with a year of 2020 and EmployeeTwo column also has 0001 with a year of 2020由于 EmployeeOne 列有 0001 和 2020 年,而 EmployeeTwo 列也有 0001 和 2020 年

I've tried putting in the place the following composite keys我尝试将以下复合键放在该位置

  • Year & EmployeeOne年份 & EmployeeOne
  • Year & EmployeeTwo年份 & EmployeeTwo
  • Year & EmployeeOne & EmployeeTwo年份 & EmployeeOne & EmployeeTwo

But having no joy但没有快乐

If its not possible with Table Constraints then you can do validation in trigger.如果不能使用表约束,那么您可以在触发器中进行验证。

This is trivial if your data model has (Year, Employee, Role) instead of (Year, EmployeeOne, EmployeeTwo).如果您的数据 model 具有 (Year, Employee, Role) 而不是 (Year, EmployeeOne, EmployeeTwo),这很简单。 Then a key of (Year,Employee) ensures this constraint.然后 (Year,Employee) 的键确保了这个约束。

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

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