有没有一种方法可以使用代码优先而不是使用新代码在属性/列上创建索引IndexAttribute
?
2017年10月26日,实体框架6.2 正式发布。它包括通过Fluent API轻松定义索引的可能性。何其使用已在6.2 beta中宣布。
现在,您可以使用该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()
。
编辑#1
添加了多列索引的示例,以及有关如何将索引标记为群集的其他信息。
编辑#2
作为附加信息,在EF Core 2.1中,它与现在在EF 6.2中完全相同。
这是MS Doc articile作为参考。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句