Creating a UNIQUE Filtered Index for NULL values on Entity Framework

一世执手 提交于 2019-12-06 08:02:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!