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

后端 未结 3 1949
我寻月下人不归
我寻月下人不归 2020-12-18 17:20

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 ear

3条回答
  •  情深已故
    2020-12-18 18:12

    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

提交回复
热议问题