Entity Framework Core add unique constraint code-first

前端 未结 9 998
南方客
南方客 2020-12-12 17:36

I can\'t find way to add a unique constraint to my field with using attribute:

public class User
{
    [Required]
    public int Id { get; set; }

    [Requi         


        
9条回答
  •  半阙折子戏
    2020-12-12 18:23

    For someone who is trying all these solution but not working try this one, it worked for me

    protected override void OnModelCreating(ModelBuilder builder)
    {
    
        builder.Entity().Property(t => t.Email).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_EmailIndex") { IsUnique = true }));
    
    }
    

提交回复
热议问题