开发者

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜