有没有一种方法可以使用代码优先而不是使用新代码在属性/列上创建索引IndexAttribute?
有没有一种方法可以使用代码优先而不是使用新代码在属性/列上创建索引IndexAttribute?
Answers:
2017年10月26日,Entity Framework 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作为参考。
new。我会修好它。
                    当前,没有通过Fluent API创建索引的“一流支持”,但是您可以通过Fluent API进行操作,您可以将属性标记为具有Annotation API中的属性。这将允许您Index通过流畅的界面添加属性。
以下是EF发行站点上工作项中的一些示例。
在单列上创建索引:
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)));使用以上技术将导致.CreateIndex()在您为Up()下一个迁移进行脚手架时为您的函数自动创建调用(或者,如果您不使用迁移,则在数据库中自动创建调用)。
.Primarykey(x=>x.id,clustered:false)方法中声明clusered:false所创建的迁移文件中明确删除该文件
                    HasAnnotation方法,没有这样的方法。但是我找到了一个名称HasColumnAnnotation可以接受您提供的参数的方法。您需要更新答案还是我错了?
                    我创建了一些扩展方法,并将它们包装在nuget包中,以使其变得更加容易。
安装EntityFramework.IndexingExtensionsnuget软件包。
然后,您可以执行以下操作:
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课程所公开的,因此我无法将其包含在我的图书馆中。
                    没有明确的名称:
[Index]
public int Rating { get; set; } 使用特定名称:
[Index("PostRatingIndex")] 
public int Rating { get; set; }EntityFramework。它包含在该程序集中。只是对NS感到困惑。
                    instead of using the new IndexAttribute,您注意到了吗?
                    从EF 6.1开始,[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
                    实体框架6
Property(c => c.MyColumn)
        .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_MyIndex")));并使用添加:
using System.Data.Entity.Infrastructure.Annotations;
using System.ComponentModel.DataAnnotations.Schema;您可以使用INDEX数据注释 代码优先数据注释
如果您不想在POCO上使用属性,则可以始终按照以下方式进行操作:
context.Database.ExecuteSqlCommand("CREATE INDEX IX_NAME ON ..."); 您可以在自定义DbInitializer派生类中执行此语句。我不知道这样做的任何Fluent API方法。
我编写了一种扩展方法以在流利的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 });您可以使用其中之一
//索引
 this.HasIndex(e => e.IsActive)
            .HasName("IX_IsActive");要么
  this.Property(e => e.IsActive).HasColumnAnnotation(
            "Index",
            new IndexAnnotation(new IndexAttribute("IX_IsActive")));