【问题标题】:Entity Framework Code First Migration in MySql DataBaseMySql 数据库中的实体框架代码优先迁移
【发布时间】:2015-11-06 03:32:33
【问题描述】:

我使用实体框架创建代码优先模型,在将第一个版本安装到客户端后,我们发现数据库列发生了一些变化,我使用实体框架的迁移,并且您应用迁移的所有步骤,但客户端的数据库不存在上次更改的迁移历史记录并在执行时显示此消息

Table 'nameTable' already exists

Additional information: Duplicate column name 'Reference'

这个类配置代码

 internal sealed class Configuration : DbMigrationsConfiguration<GSM.DataAccess.GSMContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = false;
        SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());  
    }

    protected override void Seed(GSM.DataAccess.GSMContext context)
    {

    }
}

还有这个类第一次迁移的代码

public partial class AddMoreInformationColumn : DbMigration
{
    public override void Up()
    {

            AddColumn("dbo.People", "Reference", c => c.String(unicode: false));
            AddColumn("dbo.Products", "Reference", c => c.String(unicode: false));
            AddColumn("dbo.Products", "SalePrice", c => c.String(unicode: false));
            AddColumn("dbo.Products", "WholePrice", c => c.String(unicode: false));


    }

    public override void Down()
    {

            DropColumn("dbo.People", "Address");
            DropColumn("dbo.People", "Reference");
            DropColumn("dbo.Products", "Reference");
            DropColumn("dbo.Products", "SalePrice");
            DropColumn("dbo.Products", "WholePrice");

    }
}

【问题讨论】:

    标签: c# mysql entity-framework migration entity-framework-6


    【解决方案1】:

    关于当前的问题,客户端迁移快照似乎不同步。您可以在 Up() 方法中注释掉冲突的代码并执行更新数据库以使它们重新同步。如果您担心缺少更改,则需要使用架构比较工具 (http://www.techbubbles.com/sql-server/schema-compare-for-sql-server-in-visual-studio-2013/)。

    我会重新考虑您更新客户端(生产)数据库的策略。我们所做的是从我们的迁移中生成一个脚本,该脚本在客户端站点上运行以更新它们。见:http://cpratt.co/migrating-production-database-with-entity-framework-code-first/#at_pco=smlwn-1.0&at_si=54ad5c7b61c48943&at_ab=per-12&at_pos=0&at_tot=1

    您还必须考虑您的数据库初始化程序是什么,并将其设置为 null 或 migratetolatestversion 以进行部署。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      相关资源
      最近更新 更多