但是,今天我再次对数据库进行了更改,并进行了 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的上一次迁移回滚到第二个数据库. 更新数据库–TargetMigration< second_last_migration> 添加迁移<完整名称(包括_timestamp_of_last_migration>) 您需要包括时间戳记,以便迁移知道您要编辑现有的迁移,而不是搭建一个新的迁移.这将更新上一次迁移的元数据,以匹配当前模型. 更新数据库 来源 https://docs.microsoft.com/zh-CN/ef/ef6/modeling/code-first/migrations/teams#resolving-the-merge-conflictEF 6.2SQL Server 2012I am getting the exception:I have done my research here, here, hereI 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 changesTurning on automatic migrations and it STILL complainsI 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 1This 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> –IgnoreChangesMethod 2This 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.Update-Database –TargetMigration <second_last_migration>Add-Migration <full_name_including_timestamp_of_last_migration>You need to include the timestamp so that migrations knows you wantto edit the existing migration rather than scaffolding a new one.This will update the metadata for the last migration to match thecurrent model.Update-DatabaseSource https://docs.microsoft.com/en-gb/ef/ef6/modeling/code-first/migrations/teams#resolving-the-merge-conflict 这篇关于即使模型匹配,我也会得到-无法更新数据库以匹配当前模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-30 00:54