有没有办法使用流畅的配置在属性/列上创建索引,而不是使用新的IndexAttribute https://msdn.microsoft.com/en-us/library/jj591583(v=vs.113).aspx#Anchor_10 ?
那么 2017 年 10 月 26 日实体框架 6.2 已发布正式发布 https://blogs.msdn.microsoft.com/dotnet/2017/10/26/entity-framework-6-2-runtime-released/。
它包括一个可能性 https://github.com/aspnet/EntityFramework6/issues/274通过 Fluent API 轻松定义索引。具体使用方法已经在宣布 https://blogs.msdn.microsoft.com/dotnet/2017/05/23/announcing-ef-6-2-beta-1/#fluent-api-to-define-indexes-274在 6.2 测试版中。
现在您可以使用HasIndex()
方法,然后是IsUnique()
如果它应该是唯一索引。
只是一个小的比较(之前/之后)示例:
// before
modelBuilder.Entity<Person>()
.Property(e => e.Name)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(new IndexAttribute { IsUnique = true }));
// after
modelBuilder.Entity<Person>()
.HasIndex(p => p.Name)
.IsUnique();
// multi column index
modelBuilder.Entity<Person>()
.HasIndex(p => new { p.Name, p.Firstname })
.IsUnique();
还可以将索引标记为聚集索引.IsClustered()
.
EDIT #1
添加了多列索引的示例以及如何将索引标记为聚集的附加信息。
EDIT #2
作为附加信息,在 EF Core 2.1 中它与现在的 EF 6.2 中完全相同。
Here https://learn.microsoft.com/de-de/ef/core/modeling/indexes是 MS Doc 文章作为参考。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)