Add index with entity framework code first (CTP5)

前端 未结 4 1904
走了就别回头了
走了就别回头了 2020-12-13 01:02

Is there a way to get EF CTP5 to create an index when it creates a schema?

Update: See here for how EF 6.1 handles this (as pointed out by juFo belo

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 01:16

    You can take advantage of the new CTP5’s ExecuteSqlCommand method on Database class which allows raw SQL commands to be executed against the database.

    The best place to invoke SqlCommand method for this purpose is inside a Seed method that has been overridden in a custom Initializer class. For example:

    protected override void Seed(EntityMappingContext context)
    {
        context.Database.ExecuteSqlCommand("CREATE INDEX IX_NAME ON ...");
    }
    

提交回复
热议问题