问题描述
我正在尝试创建一个表,该表具有使用实体框架的NULL值的独特过滤索引(例如,允许空值重复)。我使用Fluent API并拥有此实体属性:
modelBuilder.Entity< Client>()。Property(c => c .Barcode)
.HasMaxLength(20)
.IsRequired()
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(new IndexAttribute(IX_ClientBarcode){ IsUnique = true}));
我发现SQL Server 2008允许使用带有过滤空值的唯一列:
CREATE UNIQUE INDEX indexName ON tableName(columns)INCLUDE includeColumns WHERE columnName IS NOT NULL
这甚至是可以吗?因为我正在使用LocalDB。
目前我不认为这是可能的:
我将使用一个IDatabaseInitializer添加使用dbContext.Database.ExecuteSqlCommand()创建数据库后的索引。希望支持将在未来的版本中发布。
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.
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.
这篇关于在实体框架上为NULL值创建独特的过滤索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!