Ed Andersen

Ed Andersen

Share this post

Ed Andersen
Ed Andersen
EF Core Migrations – creating Indexes Online depending on SQL Server edition
Copy link
Facebook
Email
Notes
More
User's avatar
Discover more from Ed Andersen
Software Engineer in Japan. Subscribe for blog posts and the occasional analysis of the important developer news.
Already have an account? Sign in

EF Core Migrations – creating Indexes Online depending on SQL Server edition

Ed Andersen's avatar
Ed Andersen
Feb 04, 2020

Share this post

Ed Andersen
Ed Andersen
EF Core Migrations – creating Indexes Online depending on SQL Server edition
Copy link
Facebook
Email
Notes
More
Share

I recently hit the classic case of trying to add Indexes to a large table. Whilst Entity Framework Core supports creating Indexes online during migrations, not all versions of SQL Server support this.

In the case that your migration contains the code:

 migrationBuilder.CreateIndex(
                 name: "IX_TableName_ColumnName",
                 table: "TableName",
                 column: "ColumnName").Annotation("SqlServer:Online", true);

This will fail hard on SQL Server Express, which you are likely using for development locally, with the error message “Online index operations can only be performed in Enterprise edition of SQL Server.”. Online index operations are available in Enterprise or luckily in my case, Azure SQL.

Whilst there is not a “feature flag” to detect the support of Online index creation, you can execute the following query to detect the edition of SQL Server your app is running on.

SELECT SERVERPROPERTY(‘EngineEdition’)

Which returns 3 for Enterprise edition or 5 for SQL Azure (full list here).

EF Core has removed the ability to easily execute scalar queries so you’ll need a small extension method:

public static class SqlQueryExtensions
    {
        public static T ExecuteScalar<T>(this DbContext context, string rawSql,
         params object[] parameters)
        {
            var conn = context.Database.GetDbConnection();
            using (var command = conn.CreateCommand())
            {
                command.CommandText = rawSql;
                if (parameters != null)
                    foreach (var p in parameters)
                        command.Parameters.Add(p);
                conn.Open();
                return (T) command.ExecuteScalar();
            }
        }

And then you can set a public static property on your migration before calling DbContext.Migrate():

var dbEngineVersion = dbContext.ExecuteScalar<int>("SELECT SERVERPROPERTY('EngineEdition')");
MyMigrationName.UseOnlineIndexCreation = dbEngineVersion == 3 || dbEngineVersion == 5;
dbContext.Database.Migrate();


public partial class MyMigrationName : Migration
{
    public static bool UseOnlineIndexCreation { get; set; }

    protected override void Up(MigrationBuilder migrationBuilder)
    {
        if (UseOnlineIndexCreation)
        {
            migrationBuilder.CreateIndex(
             name: "IX_TableName_ColumnName",
             table: "TableName",
             column: "ColumnName").Annotation("SqlServer:Online", true);
        }
        else
        {
            migrationBuilder.CreateIndex(
             name: "IX_TableName_ColumnName",
             table: "TableName",
             column: "ColumnName");
            }

        }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropIndex(
            name: "IX_TableName_ColumnName",
            table: "AuditTrail");
    }
}

Now your Index will be created Online on editions of SQL Server that support it.


Subscribe to Ed Andersen

Launched 2 years ago
Software Engineer in Japan. Subscribe for blog posts and the occasional analysis of the important developer news.

Share this post

Ed Andersen
Ed Andersen
EF Core Migrations – creating Indexes Online depending on SQL Server edition
Copy link
Facebook
Email
Notes
More
Share

Discussion about this post

User's avatar
VS Code is no longer an IDE, layoffs hit MS teams and .NET 10 Preview 4 is out
Teds Tech, May 15th 2025
May 15 • 
Ed Andersen
2

Share this post

Ed Andersen
Ed Andersen
VS Code is no longer an IDE, layoffs hit MS teams and .NET 10 Preview 4 is out
Copy link
Facebook
Email
Notes
More
1
Github Copilot for upgrading .NET projects and querying Azure - .NET Conf recap
Ted's Tech, April 25th 2025
Apr 25 • 
Ed Andersen

Share this post

Ed Andersen
Ed Andersen
Github Copilot for upgrading .NET projects and querying Azure - .NET Conf recap
Copy link
Facebook
Email
Notes
More
.NET 10 Preview 3 releases and Semantic Kernel Python catches up to .NET
Ted's Tech, April 18th 2025
Apr 18 • 
Ed Andersen

Share this post

Ed Andersen
Ed Andersen
.NET 10 Preview 3 releases and Semantic Kernel Python catches up to .NET
Copy link
Facebook
Email
Notes
More

Ready for more?

© 2025 Ed Andersen
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share

Copy link
Facebook
Email
Notes
More

Create your profile

User's avatar

Only paid subscribers can comment on this post

Already a paid subscriber? Sign in

Check your email

For your security, we need to re-authenticate you.

Click the link we sent to , or click here to sign in.