具有“ NULL”值的EF核心唯一约束

时间:2020-08-31 09:18:00

标签: entity-framework npgsql

Does EF Core allow a unique column to contain multiple nulls?的解决方案与 Microsoft SQL Server,但不支持PostgreSQL。有没有可以与PostgreSQL(Npgsql提供程序)一起使用的解决方案?

1 个答案:

答案 0 :(得分:0)

只需使用a filtered index即可根据需要指定WHERE子句:

modelBuilder.Entity<Blog>().HasIndex(b => b.SomeInt)
    .HasFilter(@"""SomeInt"" IS NOT NULL")
    .IsUnique();

这将如下创建索引:

CREATE UNIQUE INDEX "IX_Blogs_SomeInt" ON "Blogs" ("SomeInt") WHERE "SomeInt" IS NOT NULL;