EF 6.1 Unique Nullable Index

后端 未结 4 1449
Happy的楠姐
Happy的楠姐 2020-12-01 07:23

In EF 6.1 using Code First you can create Indexes using Attributes in your Entities or using the fluent API along the lines of:

 Property(x => x.PropertyN         


        
4条回答
  •  春和景丽
    2020-12-01 07:48

    In EF Core you can use the HasFilter method in the fluent API to achieve what you're looking for without adding custom SQL to the migration.

    builder.Entity()
        .HasIndex(x => x.PropertyName)
        .HasName("IX_IndexName")
        .HasFilter("PropertyName IS NOT NULL");
    

    This generates a migration like this:

    migrationBuilder.CreateIndex(
        name: "IX_IndexName",
        table: "Table",
        columns: new[] { "PropertyName" },
        filter: "PropertyName IS NOT NULL");
    

    提交回复
    热议问题