有没有一种使用流畅配置在属性/列上创建索引的方法,而不是使用新的IndexAttribute
?
有没有一种使用流畅配置在属性/列上创建索引的方法,而不是使用新的IndexAttribute
?
2017年10月26日,Entity Framework 6.2 正式发布。它通过Fluent API 方便地定义索引。如何使用已经在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()
将索引标记为聚集。
编辑 #1
添加了一个多列索引的示例,以及如何将索引标记为聚集的附加信息。
编辑 #2
额外的信息是,在EF Core 2.1中,现在与EF 6.2完全相同。
这里是MS Doc文章作为参考。
Index
属性。modelBuilder.Entity<MyEntity>()
.Property(e => e.MyProperty)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(new IndexAttribute()));
单列上的多个索引:
modelBuilder.Entity<MyEntity>()
.Property(e => e.MyProperty)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(new[]
{
new IndexAttribute("Index1"),
new IndexAttribute("Index2") { IsUnique = true }
}));
多列索引:
modelBuilder.Entity<MyEntity>()
.Property(e => e.MyProperty1)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(new IndexAttribute("MyIndex", 1)));
modelBuilder.Entity<MyEntity>()
.Property(e => e.MyProperty2)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(new IndexAttribute("MyIndex", 2)));
Up()
函数中自动为您创建.CreateIndex()
调用(或者如果您不使用迁移,则会在数据库中自动创建)。.Primarykey(x=>x.id,clustered:false)
方法中声明 clustered:false
来实现。 - JoyHasAnnotation
方法,但是没有这样的方法。不过,我找到了一个名为HasColumnAnnotation
的方法,它接受你所提供的参数。你需要更新你的答案吗?还是我弄错了? - Hakan Fıstık我创建了一些扩展方法并将它们封装在一个NuGet包中,使这个过程变得更加容易。
安装EntityFramework.IndexingExtensions
NuGet包。
然后你可以执行以下操作:
public class MyDataContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>()
.HasIndex("IX_Customers_Name", // Provide the index name.
e => e.Property(x => x.LastName), // Specify at least one column.
e => e.Property(x => x.FirstName)) // Multiple columns as desired.
.HasIndex("IX_Customers_EmailAddress", // Supports fluent chaining for more indexes.
IndexOptions.Unique, // Supports flags for unique and clustered.
e => e.Property(x => x.EmailAddress));
}
}
项目和源代码在这里。享受!
IndexAttribute
类没有暴露这个功能,所以我不能在我的库中包含它。 - Matt Johnson-Pint[Index]
。[Index(IsUnique = true)]
创建唯一索引。public class User
{
public int UserId { get; set; }
[Index(IsUnique = true)]
[StringLength(200)]
public string Username { get; set; }
public string DisplayName { get; set; }
}
[Index("IX_BlogIdAndRating", 2)]
public int Rating { get; set; }
[Index("IX_BlogIdAndRating", 1)]
public int BlogId { get; set; }
这里是来自Microsoft的参考。 - Darie Dorlus没有明确的名称:
[Index]
public int Rating { get; set; }
有一个特定的名称:
[Index("PostRatingIndex")]
public int Rating { get; set; }
EntityFramework
。它包含在那个程序集中。只是对 NS 感到困惑。 - Hamed Zakery Miab实体框架 6
Property(c => c.MyColumn)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_MyIndex")));
然后添加使用:
using System.Data.Entity.Infrastructure.Annotations;
using System.ComponentModel.DataAnnotations.Schema;
如果您不想在 POCO 上使用属性,那么您可以按照以下方式进行:
context.Database.ExecuteSqlCommand("CREATE INDEX IX_NAME ON ...");
DbInitializer
派生类中执行此语句。但我不知道任何使用Fluent API的方法来执行此操作。您可以使用以下其中之一
// 索引
this.HasIndex(e => e.IsActive)
.HasName("IX_IsActive");
this.Property(e => e.IsActive).HasColumnAnnotation(
"Index",
new IndexAnnotation(new IndexAttribute("IX_IsActive")));
我编写了一个扩展方法,用于流畅地使用EF以避免额外的代码:
public static PrimitivePropertyConfiguration HasIndexAnnotation(
this PrimitivePropertyConfiguration primitivePropertyConfiguration,
IndexAttribute indexAttribute = null
)
{
indexAttribute = indexAttribute ?? new IndexAttribute();
return primitivePropertyConfiguration
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(indexAttribute)
);
}
Property(t => t.CardNo)
.HasIndexAnnotation();
Property(t => t.CardNo)
.HasIndexAnnotation(new IndexAttribute("IX_Account") { IsUnique = true });