gpt4 book ai didi

c# - 即使模型匹配我得到 - 无法更新数据库以匹配当前模型

转载 作者:太空狗 更新时间:2023-10-30 01:00:12 26 4
gpt4 key购买 nike

  • EF 6.2
  • SQL Server 2012

我遇到异常:

Unable to update database to match the current model because there are pending changes and automatic migration is disabled...

我已经做了我的研究here , here , here

我有一个解决方案可以运行很长时间,没有任何问题。事实上,几天前我对数据库进行了更改,使用标准的 add-migration 后跟 update-database 一切正常,没有任何问题。

但是今天我再次更改了数据库并执行了 add-migration 然后是 update-database。但是当我运行该应用程序时,出现上述错误。

我通过在我的 Application_Start 中包含以下内容来确保迁移运行:

ConfigurationPlatform configurationPlatform = new ConfigurationPlatform();
DbMigrator migratorPlatform = new DbMigrator(configurationPlatform);
migratorPlatform.Update();

配置类如下所示:

public sealed class ConfigurationPlatform : DbMigrationsConfiguration<TreasurePlatformDbContext>
{
public ConfigurationPlatform()
{
AutomaticMigrationsEnabled = false;
AutomaticMigrationDataLossAllowed = false;
ContextKey = "TreasurePlatform";
}

protected override void Seed(TreasurePlatformDbContext aContext)
{
// This method will be called every time after migrating to the latest version.
// You can add any seed data here...
}
}

我也试过:

  • 再次运行 add-migration 但没有产生任何变化
  • 打开自动迁移,它仍然提示

我相信 POCO 表模型与数据库中的内容相匹配。有没有人有任何建议或认为我可以尝试?

最佳答案

Entity Framework 在每次迁移中存储模型的快照。听起来您的快照与当前模型不同步。有两种潜在的方法可以解决这个问题。

方法一

这将创建一个空白的虚拟迁移,其中包含您最新模型的快照,但不包含实际代码。不幸的是,这确实意味着您的项目中会有额外的代码。

运行 Add-Migration <pick_a_name> –IgnoreChanges

方法二

这将回滚您的数据库,然后使用更新的快照重新创建迁移。

只有在您尚未将迁移推送到 Git 或更新任何其他数据库时,您才能执行此操作。否则,任何其他更新的数据库也需要回滚到此过程第 1 步上次迁移后的第二个数据库。

  1. Update-Database –TargetMigration <second_last_migration>

  2. Add-Migration <full_name_including_timestamp_of_last_migration>

    您需要包含时间戳,以便迁移知道您想要编辑现有的迁移而不是构建新的迁移。这将更新上次迁移的元数据以匹配当前模型。

  3. Update-Database

来源https://learn.microsoft.com/en-gb/ef/ef6/modeling/code-first/migrations/teams#resolving-the-merge-conflict

关于c# - 即使模型匹配我得到 - 无法更新数据库以匹配当前模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48484528/

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