Create UNIQUE Filtered Index for NULL Values ​​in Entity Framework

I am trying to create a table with UNIQUE Filtered index for NULL values ​​(e.g. Allow Duplicate Nulls) using Entity Framework.

I am using Fluent API and have this property:

modelBuilder.Entity<Client>().Property(c => c.Barcode)
            .HasMaxLength(20)
            .IsRequired()
            .HasColumnAnnotation(
                IndexAnnotation.AnnotationName,
                new IndexAnnotation(new IndexAttribute("IX_ClientBarcode") { IsUnique = true }));

      

I found that SQL Server 2008 allows this for unique zero-filtered columns:

CREATE UNIQUE INDEX indexName ON tableName(columns) INCLUDE includeColumns WHERE columnName IS NOT NULL

      

Would it even be possible? As I am using LocalDB.

+3


source to share


1 answer


I don't think this is possible at the moment:

http://gavindraper.com/2014/06/26/entity-framework-fluent-api-and-indexing/



I am going to use the IDatabaseInitializer to add indexes after the database is created using dbContext.Database.ExecuteSqlCommand (). Hopefully support will come in a future release.

+2


source







All Articles