Error deploying to SQL Azure using EF 6 alpha3 Code First and Migrations creating __MigrationHistory table

azure-sql-database entity-framework entity-framework-6

Question

I'm using EF 6 alpha 3 code first. When I try to create the database on SQL Azure running the Update-Database command I get the following error:

Tables without a clustered index are not supported in this version of SQL Server. Please create a clustered index and try again.

I tracked down the error to the __MigrationHistory table creation sql command.

CREATE TABLE [dbo].[__MigrationHistory] (
    [MigrationId] [nvarchar](255) NOT NULL,
    [ContextKey] [nvarchar](512) NOT NULL,
    [Model] [varbinary](max) NOT NULL,
    [ProductVersion] [nvarchar](32) NOT NULL,
    CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY NONCLUSTERED ([MigrationId], [ContextKey])
)

Anyone has any idea about how can I workaround this problem?

Thanks,

Accepted Answer

This is a bug in Alpha 3 - Sorry for the inconvenience.

There is a pretty easy workaround:

1) Create a custom migration SQL generator:

public class AzureSqlGenerator : SqlServerMigrationSqlGenerator
{
    protected override void Generate(CreateTableOperation createTableOperation)
    {
        if ((createTableOperation.PrimaryKey != null)
            && !createTableOperation.PrimaryKey.IsClustered)
        {
            createTableOperation.PrimaryKey.IsClustered = true;
        }

        base.Generate(createTableOperation);
    }
}

2) Register the custom generator in your migrations configuration:

internal sealed class Configuration : DbMigrationsConfiguration<MyContext> 
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;

        SetSqlGenerator("System.Data.SqlClient", new AzureSqlGenerator());
    }

    protected override void Seed(MyContext context)
    {
    }
}



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why