EF6 IsUnique属性和多个空值

时间:2014-10-09 13:48:24

标签: entity-framework frameworks null entity unique

我在EF6中使用代码优先模型。是否可以使用“不关心”(允许)多个空值来创建唯一索引?类似于sql命令:

CREATE UNIQUE NONCLUSTERED INDEX idx_yourcolumn_notnull
ON YourTable(yourcolumn)
WHERE yourcolumn IS NOT NULL;

1 个答案:

答案 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");
    }
}