无法使用MySql运行代码优先迁移 - RenameColumn

时间:2012-06-03 15:14:42

标签: mysql entity-framework ef-code-first entity-framework-4.3 ef-migrations

我遇到了一个无法通过网络找到答案的问题。

我正在使用CodeFirst EF 4.3.1迁移MySQL。

我的MySQL提供商是Devart。

对现有数据库运行Add-Migration后,我得到了以下代码:

public partial class ChangeSet_1231 : DbMigration
{
    public override void Up()
    {
        RenameColumn(table: "RW_TTaskInstanceProperties", name: "TaskInstance_TaskInstanceId", newName: "TaskInstanceId");
    }

    public override void Down()
    {
        RenameColumn(table: "RW_TTaskInstanceProperties", name: "TaskInstanceId", newName: "TaskInstance_TaskInstanceId");
    }
}

运行Update-Database会导致以下错误:

PM> Update-Database -verbose –startupprojectname "RTDataAccess"
Using NuGet project 'RTDataAccess'.
Target database is: 'rsruntime' (DataSource: localhost, Provider: Devart.Data.MySql, Origin: Explicit).
Applying explicit migrations: [201205311312361_ChangeSet_1231].
Applying explicit migration: 201205311312361_ChangeSet_1231.
ALTER TABLE RW_TTaskInstanceProperties RENAME COLUMN TaskInstance_TaskInstanceId TO TaskInstanceId

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'COLUMN TaskInstance_TaskInstanceId TO TaskInstanceId' at line 2

通过查看错误详细信息,我看到RenameColumn被转换为MsSql命令,而不是MySql命令,所以难怪它会报告语法错误。

任何想法如何解决?

我知道我可以使用Update-Database -script,然后编辑脚本以适应MySql并运行它,但我更喜欢让Update-Database命令工作......

感谢。

回应拉迪斯拉夫的问题:

是的,我为MySQL迁移注册了Devart的SQL生成器。 我的Configuration类看起来像这样:

using Devart.Data.MySql.Entity.Configuration;
using Devart.Data.MySql.Entity.Migrations;

internal sealed class Configuration : DbMigrationsConfiguration<RTDataAccess.RTContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;

        MySqlEntityProviderConfig.Instance.Workarounds.IgnoreSchemaName = true;
        var connectionInfo = MySqlConnectionInfo.CreateConnection("Server=xxxx;Port=yyyy;Database=rsruntime;Uid=zzzz;Pwd=wwww;charset=utf8;");
        this.TargetDatabase = connectionInfo;
        this.SetSqlGenerator(connectionInfo.GetInvariantName(), new MySqlEntityMigrationSqlGenerator());
    }

    protected override void Seed(RTDataAccess.RTContext context)
    {
    }
}

1 个答案:

答案 0 :(得分:3)