Adding index to a table

后端 未结 2 994
梦如初夏
梦如初夏 2020-12-03 12:03

I have a table Person: id, name

I often have queries like:

select * from Person where name Like \"%abc%\".
相关标签:
2条回答
  • 2020-12-03 12:39

    in EF 6 you can create indexes like this

       Property(t => t.TotalValue)
                .HasColumnAnnotation(
                    IndexAnnotation.AnnotationName,
                    new IndexAnnotation(new IndexAttribute("IX_inventory_import_total_value", 1))
                );
    
    0 讨论(0)
  • 2020-12-03 12:48

    Like operator can be performed with Contains function:

    var query = from p in context.Persons
                where p.Name.Contains("abc")
                select p;
    

    Index must be added by SQL - there is no special construction in EF to create index. You can execute this SQL from DB initialization.

    First you must implement custom initializer:

    public class MyInitializer : CreateDatabaseIfNotExists<MyContext>
    {
      protected override void Seed(MyContext context)
      {
        context.Database.SqlCommand("CREATE INDEX IX_Person_Name ON Person (Name)");
      }
    }
    

    Then you must register new initializer:

    DbDatabase.SetInitializer<MyContext>(new MyInitializer());
    
    0 讨论(0)
提交回复
热议问题