Entity Framework Code First Fluent Api: Adding Indexes to columns

前端 未结 15 1380
一生所求
一生所求 2020-11-30 17:43

I\'m running EF 4.2 CF and want to create indexes on certain columns in my POCO objects.

As an example lets say we have this employee class:

public c         


        
15条回答
  •  情书的邮戳
    2020-11-30 18:12

    I've also looked into this recently and found no other way, so I settled with creating indexes when seeding the database:

    public class MyDBInitializer : DropCreateDatabaseIfModelChanges
    {
        private MyContext _Context;
    
        protected override void Seed(MyContext context)
        {
            base.Seed(context);
            _Context = context;
    
            // We create database indexes
            CreateIndex("FieldName", typeof(ClassName));
    
            context.SaveChanges();
        }
    
        private void CreateIndex(string field, Type table)
        {
            _Context.Database.ExecuteSqlCommand(String.Format("CREATE INDEX IX_{0} ON {1} ({0})", field, table.Name));
        }    
    }   
    

提交回复
热议问题