Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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,

like image 710
Bruno Moscão Avatar asked Dec 03 '25 05:12

Bruno Moscão


1 Answers

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)
    {
    }
}
like image 65
Andrew Peters Avatar answered Dec 04 '25 18:12

Andrew Peters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!