How to create a unique constraint for a type string field in EF 6?

First off, I'm new to MVC.

I have a Project table in which there are ProjectID, ProjectNumber and ProjectDescription fields. ProjectId is an entityKey of type int, ProjectNumber must be a unique constraint.

How to do this in the infrastructure of entity 6.1.3?

in my class Project I have

        public int ProjectID { get; set; }
        [Index(IsUnique = true)]
        [StringLength(200)]
        public string ProjectNumber { get; set; }
        public string ProjectDescription { get; set; }

when I create the database from the model, the field is not set as unique in the database.

what am I doing wrong?

+4
source share
1 answer

Based on the answer in the first column of Entity Framework code

Try this code:

public int ProjectID { get; set; }
[Index("ProjectNumber_Index", IsUnique = true)]
[MaxLength(200)]
public string ProjectNumber { get; set; }
public string ProjectDescription { get; set; }

, nvarchar (MAX) SQL Server Entity Framework.

+2

Source: https://habr.com/ru/post/1654512/


All Articles