简体   繁体   English

独特的约束实体框架

[英]Unique constraint entity framework

I'm using MS SQL Server 2012 + the Entity Framework 6.1.1. 我正在使用MS SQL Server 2012 +实体框架6.1.1。 I have created a model using the designer. 我已经使用设计器创建了一个模型。 Now, I would like to add a unique to an attribute. 现在,我想为属性添加唯一性。 How can I achieve this using the designer / keep the model generation? 如何使用设计器实现这一目标/保持模型生成?

Edit: I do the model first approach 编辑:我做模型的第一种方法

Thank you 谢谢

Read this! 读这个! It should help you in the right direction... 它应该在正确的方向上帮助您...

The idea is to use code like this: 这个想法是使用这样的代码:

CreateIndex("dbo.Users","Username",true);

Which is a EF method. 这是EF方法。 I don't think it's possible in the designer then... 我认为设计师不可能做到这一点...

EDIT 编辑

Found this post that states that unique constraints are possible (not in the designer, but in code) like this: 发现这篇文章指出唯一约束是可能的(不是在设计器中,而是在代码中),如下所示:

[Index(IsUnique = true)]
public string EmailAddress { get; set; }

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

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