Is it possible to set a unique constraint using Entity Framework Code First?

52,665

Solution 1

It appears that the unique constraint feature that was scheduled to release with Version 6 got pushed to 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; }

OR

You can use Fluent API as shown here in MSDN

Solution 2

Let's say that you want to add the Unique constraint on only one attribute, you can do as following, starting from EF6.1

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

If you have multiple fields that are related to the same index then you are going to use:

Multiple-Column Indexes

Indexes that span multiple columns are specified by using the same name in multiple Index annotations for a given table. When you create multi-column indexes, you need to specify an order for the columns in the index. For example, the following code creates a multi-column index on Rating and BlogId called IX_BlogAndRating. BlogId is the first column in the index and Rating is the second.

public class Post 
{ 
    public int Id { get; set; } 
    public string Title { get; set; } 
    public string Content { get; set; } 
    [Index("IX_BlogIdAndRating", 2)] 
    public int Rating { get; set; } 
    [Index("IX_BlogIdAndRating", 1)] 
    public int BlogId { get; set; } 
}

Please refer to this link for further information.

Solution 3

Use this to define it in config class, if you're avoiding using annotations:

public class YourTableConfig : EntityTypeConfiguration<YourTableEntity>
{
  public YourTableConfig()
  {
    ToTable("YourTableDbName");

    HasKey(u => u.Id);

    Property(c => c.CompanyId).HasColumnType("nvarchar").HasMaxLength(9).IsRequired();

    HasIndex(x => x.CompanyId).IsUnique(); // This sets the unique index
  }
}
Share:
52,665
subi_speedrunner
Author by

subi_speedrunner

Curious, day- dreamer, explorer

Updated on July 09, 2022

Comments

  • subi_speedrunner
    subi_speedrunner almost 2 years

    I want to enforce Unique constraint in a table & I am using Entity Framework Code-First.

    Is it possible to add a unique constraint using EF 6 as i believe in earlier versions it was not possible.

  • subi_speedrunner
    subi_speedrunner over 9 years
    Thanks for the Update,so creating this unique index has the same effect as of creating unique constraint
  • SBirthare
    SBirthare over 9 years
    Yes it will create an unique index for the column in the DB.
  • Shimmy Weitzhandler
    Shimmy Weitzhandler almost 9 years
    Can't seem to find it in EF7 - DNX.
  • Rolf
    Rolf about 8 years
  • Bastien Vandamme
    Bastien Vandamme almost 5 years
    Why do you start naming with "IX_" ?
  • Mike Smith - MCT
    Mike Smith - MCT over 4 years
    "IX_" is just a convention used in SQL Server. For example, "PK_" is the common prefix for a primary key index.