即使模型匹配,我也会得到-无法更新数据库以匹配当前模型 [英] Even though model matches I am getting - Unable to update database to match the current model

查看:58
本文介绍了即使模型匹配,我也会得到-无法更新数据库以匹配当前模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • EF 6.2
  • SQL Server 2012

我遇到了例外情况:

由于存在以下原因,无法更新数据库以匹配当前模型即将发生的更改,自动迁移已禁用...

我已经完成了研究此处,此处

我有一个长期有效的解决方案,没有任何问题.实际上,几天前我对数据库进行了更改,并且使用标准的 add-migration 和紧随其后的 update-database 都很好,一切都很好.

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

我通过在Application_Start中添加以下内容来确保运行迁移:

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

,配置类如下所示:

 公共密封类ConfigurationPlatform:DbMigrationsConfiguration< TreasurePlatformDbContext>{公共ConfigurationPlatform(){AutomaticMigrationsEnabled = false;AutomaticMigrationDataLossAllowed = false;ContextKey ="TreasurePlatform";}受保护的重写void Seed(TreasurePlatformDbContext aContext){//迁移到最新版本后,将每次调用此方法.//您可以在此处添加任何种子数据...}} 

我也尝试过:

  • 再次运行add-migration,但不会产生任何变化
  • 启用自动迁移后,它仍然会抱怨

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

解决方案

实体框架在每次迁移中都存储模型的快照.听起来您的快照已与当前模型不同步.有两种潜在的方法可以解决此问题.

方法1

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

运行添加迁移< pick_a_name>–IgnoreChanges

方法2

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

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

  1. 更新数据库–TargetMigration< second_last_migration>

  2. 添加迁移<完整名称(包括_timestamp_of_last_migration>)

    您需要包括时间戳记,以便迁移知道您要编辑现有的迁移,而不是搭建一个新的迁移.这将更新上一次迁移的元数据,以匹配当前模型.

  3. 更新数据库

来源 https://docs.microsoft.com/zh-CN/ef/ef6/modeling/code-first/migrations/teams#resolving-the-merge-conflict

  • EF 6.2
  • SQL Server 2012

I am getting the exception:

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

I have done my research here, here, here

I have had a solution working for a long time now with no issues. And in fact I made a change to the database a couple of days ago and all was well using the standard add-migration followed by update-database with no issues.

However today I made a change to the database again and did add-migration followed by update-database. But when I run the application I get the above error.

I make sure the migrations run by including the following in my Application_Start:

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

and the configuration class looks as follows:

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...
    }
}

I have also tried:

  • Running add-migration again but it produces NO changes
  • Turning on automatic migrations and it STILL complains

I am confident the POCO table models match what is in the database. Has anyone got any suggestions or thinks I can try?

解决方案

Entity Framework stores a snapshot of the model in each migration. It sounds like your snapshot has become out of sync with your current model. There are two potential ways to fix this.

Method 1

This will create a blank dummy migration which contains a snapshot of your latest model but not actual code. Unfortunately it does mean you will have additional code in your project.

Run Add-Migration <pick_a_name> –IgnoreChanges

Method 2

This will rollback your database and then recreate the migration with an updated snapshot.

You can only do this if you haven't pushed the migration to Git or updated any other databases. Otherwise any other updated databases will also need to be rolled back to the second from last migration on step 1 of this process.

  1. Update-Database –TargetMigration <second_last_migration>

  2. Add-Migration <full_name_including_timestamp_of_last_migration>

    You need to include the timestamp so that migrations knows you want to edit the existing migration rather than scaffolding a new one. This will update the metadata for the last migration to match the current model.

  3. Update-Database

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

这篇关于即使模型匹配,我也会得到-无法更新数据库以匹配当前模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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