我在EF6中使用代码优先模型。是否可以使用“不关心”(允许)多个空值来创建唯一索引?类似于sql命令:
CREATE UNIQUE NONCLUSTERED INDEX idx_yourcolumn_notnull
ON YourTable(yourcolumn)
WHERE yourcolumn IS NOT NULL;
答案 0 :(得分:6)
您可以在迁移中尝试直接SQL,如:
public partial class MyMigration: DbMigration
{
public override void Up()
{
Sql(@"CREATE UNIQUE NONCLUSTERED INDEX idx_yourcolumn_notnull ON YourTable(yourcolumn) WHERE yourcolumn IS NOT NULL;");
}
public override void Down()
{
DropIndex("SGA.Empresas", "idx_yourcolumn_notnull");
}
}