问题
I'm trying to create a table that has a UNIQUE Filtered Index for NULL values (eg. Allow Null values to be duplicates) using Entity Framework.
I am using Fluent API and have this entity 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 columns with filtered nulls:
CREATE UNIQUE INDEX indexName ON tableName(columns) INCLUDE includeColumns WHERE columnName IS NOT NULL
Would this even be posible? Since I am using LocalDB.
回答1:
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 an IDatabaseInitializer to add the indexes following database creation using dbContext.Database.ExecuteSqlCommand(). Hopefully support will come in a future release.
来源:https://stackoverflow.com/questions/27555538/creating-a-unique-filtered-index-for-null-values-on-entity-framework