使用EF 6 alpha3 Code First和Migrations创建__MigrationHistory表将部署到SQL Azure时发生错误 [英] Error deploying to SQL Azure using EF 6 alpha3 Code First and Migrations creating __MigrationHistory table

查看:276
本文介绍了使用EF 6 alpha3 Code First和Migrations创建__MigrationHistory表将部署到SQL Azure时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用EF 6 alpha 3代码。
当我尝试在运行Update-Database命令的SQL Azure上创建数据库时,我收到以下错误:



不支持没有聚簇索引的表,这个版本的SQL Server。请创建一个聚集索引,然后重试。



我将错误跟踪到__MigrationHistory表创建sql命令。

  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])

任何人都不知道如何解决这个问题?



谢谢,

解决方案

这是一个错误在Alpha 3 - 对不便给您带来不便。



有一个非常容易的解决方法:



1)创建自定义迁移SQL生成器:

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

base.Generate(createTableOperation);
}
}

2)在迁移配置中注册自定义生成器:

 内部密封类配置:DbMigrationsConfiguration< MyContext> 
{
public Configuration()
{
AutomaticMigrationsEnabled = true;

SetSqlGenerator(System.Data.SqlClient,新的AzureSqlGenerator());
}

protected override void Seed(MyContext context)
{
}
}


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,

解决方案

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)
    {
    }
}

这篇关于使用EF 6 alpha3 Code First和Migrations创建__MigrationHistory表将部署到SQL Azure时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆