Annotation to create Index Unique that accepts null with EF Code First

0

I want to create the following Index in a table by annotation:

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

SQL source above .

The maximum you can do is create an index unique, but it does not support two nulls.

Would this be possible with EF6 and SQLServer 2012?

    
asked by anonymous 11.08.2016 / 15:20

1 answer

1

Ricardo, unfortunately I do not think it is possible, but through Migrations it is possible to create a contour solution.

After using the -EnableMigration and add-migration %Migration Name% commands, open the newly created migration file and edit the following line within the Up method:

Sql(string.Format(@"CREATE UNIQUE NONCLUSTERED INDEX IXCU_{0}_{1}_notnull
ON {0}({1}) WHERE yourcolumn IS NOT NULL", nomeTabela, nomeColuna);

If there is already an index with the same database name, be sure to add the following line in the Down method:

DropIndex(nomeTabela, string.Format("IXCU_{0}_{1}_notnull", nomeTabela, nomeColuna));

Now you can run update-database .

    
11.08.2016 / 15:37