是否可以使用 Entity Framework Code First 设置唯一约束? [英] Is it possible to set a unique constraint using Entity Framework Code First?
本文介绍了是否可以使用 Entity Framework Code First 设置唯一约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在表中强制执行唯一约束 &我正在使用实体框架代码优先.
I want to enforce Unique constraint in a table & I am using Entity Framework Code-First.
是否可以使用 EF 6 添加唯一约束,因为我相信在早期版本中这是不可能的.
Is it possible to add a unique constraint using EF 6 as i believe in earlier versions it was not possible.
推荐答案
看来独特的约束特征计划与版本 6 一起发布被推送到 6.1.
It appears that the unique constraint feature that was scheduled to release with Version 6 got pushed to 6.1.
使用 EF 6.1,您可以使用 Index 属性定义约束,如下所示:
With EF 6.1, you can define a constraint using the Index attribute as shown below:
[Index("IX_FirstAndSecond", 1, IsUnique = true)]
public int FirstColumn { get; set; }
[Index("IX_FirstAndSecond", 2, IsUnique = true)]
public int SecondColumn { get; set; }
或
您可以使用 MSDN
这篇关于是否可以使用 Entity Framework Code First 设置唯一约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文