Does EF Core allow a unique column to contain multiple nulls?

后端 未结 1 727
灰色年华
灰色年华 2020-12-11 18:00

My entity has a property which is allowed to be null. BUT, if it isn\'t null, then it must be unique. In other words, the column is unique but allows multiple nulls.

相关标签:
1条回答
  • 2020-12-11 18:20

    Yes, you can do that with EF Core, as a Unique index by default is created as a filtered index (WHERE ... IS NOT NULL)

    config.Entity<Product>()
            .HasIndex(b => b.ProductId)
            .IsUnique();
    

    https://github.com/aspnet/EntityFramework/pull/2868

    0 讨论(0)
提交回复
热议问题