简体   繁体   English

如何首先在EF代码中为模型中的列添加唯一键

[英]How to add Unique Key for a column in a model in EF code first

I am trying to make a column as Unique key in my model. 我试图在我的模型中将列作为唯一键。

my code is : 我的代码是:

public class Customer
{
    [Index("abcd",IsUnique = true)]
    public int CustomerID { get; set; }
}

and I am using following references: 我正在使用以下参考:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

Still i am getting 我还在

Error   103 The type or namespace name 'IndexAttribute' could not be found (are you missing a using directive or an assembly reference?)    

Am i missing anything? 我错过了什么吗? Iam using EF 6.1 version 我使用EF 6.1版本

Thanks in advance 提前致谢

Rephrase my answer. 改写我的回答。 (apologies if shortened URL's are now allowed) (如果现在允许缩短的URL,则道歉)

In .net 4.5 the Index attribute only exists in the file (EntityFramework.dll) 在.net 4.5中,Index属性仅存在于文件中(EntityFramework.dll)

The Dll System.ComponentModel.DataAnnotations.Schema does not contain the Index attribute Dll System.ComponentModel.DataAnnotations.Schema不包含Index属性

The EntityFramework.dll version 6 or later contains this attribute EntityFramework.dll版本6或更高版本包含此属性

Browse to the file EntityFramework.dll and add a reference the error will vanish and the code will compile. 浏览到文件EntityFramework.dll并添加一个错误将消失的引用,代码将编译。

The namespace System.ComponentModel.DataAnnotations.Schema is also part of this EntityFramework.dll 命名空间System.ComponentModel.DataAnnotations.Schema也是此EntityFramework.dll的一部分

I assume its a blunder by Microsoft and may be corrected in .net 5 我认为这是微软的错误,可能会在.net 5中得到纠正

  1. Right click Project Reference section 右键单击Project Reference部分

  2. Then Click Add Reference in that popup left menu select Assemblies 然后在弹出的左侧菜单中单击Add Reference选择Assemblies

  3. Check System.ComponentModel.DataAnnotations dll 检查System.ComponentModel.DataAnnotations dll

  4. Import following namespaces in code 在代码中导入以下命名空间

    using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema;

  5. Install Entity Framework for this project 为此项目安装实体框架

You need to ensure that you've got EntityFramework referenced in the project that your model class is in - I moved the model classes out of my MVC website into a separate DLL but I assumed that because IndexAttribute is in the System.ComponentModel.DataAnnotations.Schema namespace that it was in the System.ComponentMode.DataAnnotations assembly - it's actually in the EntityFramework assembly, so you need the reference. 您需要确保在模型类所在的项目中引用了EntityFramework - 我将模型类从我的MVC网站移到了一个单独的DLL中,但我认为因为IndexAttribute在System.ComponentModel.DataAnnotations中。它位于System.ComponentMode.DataAnnotations程序集中的Schema名称空间 - 它实际上位于EntityFramework程序集中,因此您需要引用。

Thanks to Animuson for pointing this out. 感谢Animuson指出这一点。

Solution: Just install the Entity Framework in each project you need to use IndexAttribute. 解决方案:只需在每个需要使用IndexAttribute的项目中安装Entity Framework。


Step-by-step instructions: 分步说明:

  • Open Package Manager Console 打开包管理器控制台
  • Copy-paste this: Install-Package EntityFramework -Version 6.2.0 -ProjectName MyProject 复制粘贴: Install-Package EntityFramework -Version 6.2.0 -ProjectName MyProject
  • substitute "MyProject" with your project and press enter 将“MyProject”替换为您的项目,然后按Enter键

More infos: https://www.nuget.org/packages/EntityFramework/ 更多信息: https//www.nuget.org/packages/EntityFramework/

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

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