是否有可能设置使用实体框架code首先唯一约束? [英] Is it possible to set a unique constraint using Entity Framework Code First?

查看:129
本文介绍了是否有可能设置使用实体框架code首先唯一约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要强制表和安培唯一约束;我使用实体框架code-第一。

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,您可以使用索引属性定义一个约束,如下所示:

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; }

您可以用流利的API如下所示在 MSDN

You can use Fluent API as shown here in MSDN

这篇关于是否有可能设置使用实体框架code首先唯一约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆