What is the best practice to programmatically set SQL indexes with Entity-Framework-Code-First
I am using Asp.Net MVC 3,开发者_如何学Go Entity-Framework 4.1 and was wondering if there is any useful way to set SQL indexes programmatically on some columns in my model.
You can setup a Database Initializer to create indexes
public class MyInitializer : CreateDatabaseIfNotExists<MyDbContext>
{
protected override void Seed(MyDbContext context)
{
//your logic here
context.Database.ExecuteSqlCommand("CREATE INDEX IX_Products_Code ON dbo.Products ( Code )");
}
}
Then you can setup the initializer on startup
Database.SetInitializer<ProductCatalog>(new MyInitializer());
精彩评论