gpt4 book ai didi

c# - MySql 数据库中的 Entity Framework 代码优先迁移

转载 作者:行者123 更新时间:2023-11-28 23:54:51 26 4
gpt4 key购买 nike

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

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");

}
}

最佳答案

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

我会重新考虑您更新客户端 (PRODUCTION) 数据库的策略。我们所做的是从我们的迁移中生成一个脚本,该脚本在客户端站点上运行以更新它们。请参阅: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 以进行部署。

关于c# - MySql 数据库中的 Entity Framework 代码优先迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31983597/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com