How to create index in Entity Framework 6.2 with code first

前端 未结 10 433
不知归路
不知归路 2020-11-29 19:16

Is there a way to create an index on a property/column using code-first, instead of using the new IndexAttribute ?

相关标签:
10条回答
  • 2020-11-29 19:41

    Entity Framework 6

    Property(c => c.MyColumn)
            .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_MyIndex")));
    

    And add using:

    using System.Data.Entity.Infrastructure.Annotations;
    using System.ComponentModel.DataAnnotations.Schema;
    
    0 讨论(0)
  • 2020-11-29 19:41

    If you don't want to use attributes on your POCO's, then you can always do it like the following:

    context.Database.ExecuteSqlCommand("CREATE INDEX IX_NAME ON ..."); 
    

    You can execute this statement in your custom DbInitializer derived class. I don't know any Fluent API way of doing this though.

    0 讨论(0)
  • 2020-11-29 19:47

    You can use the INDEX data annotaion Code First Data Annotations

    0 讨论(0)
  • 2020-11-29 19:49

    You can use one of this

    // Indexes

     this.HasIndex(e => e.IsActive)
                .HasName("IX_IsActive");
    

    or

      this.Property(e => e.IsActive).HasColumnAnnotation(
                "Index",
                new IndexAnnotation(new IndexAttribute("IX_IsActive")));
    
    0 讨论(0)
  • 2020-11-29 19:51

    Well 26.10.2017 Entity Framework 6.2 was officially released. It includes a possibility to define indexes with ease via Fluent API. Ho it is to use was already announced in the beta of 6.2.

    Now you can use the HasIndex() method, followed by IsUnique() if it should be an unique index.

    Just a small comparison (before/after) example:

    // 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();
    

    It is also possible to mark the index as clustered with .IsClustered().


    EDIT #1

    Added an example for multi column index and additional information how to mark an index as clustered.


    EDIT #2

    As additional information, in EF Core 2.1 it is exactly the same like in EF 6.2 now.
    Here is the MS Doc artcile as reference.

    0 讨论(0)
  • 2020-11-29 19:55

    I've created a some extension methods and wrapped them in a nuget package to make this much easier.

    Install the EntityFramework.IndexingExtensions nuget package.

    Then you can do the following:

    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)); 
      }
    }
    

    The project and source code are here. Enjoy!

    0 讨论(0)
提交回复
热议问题