我得到了例外:
无法更新数据库以匹配当前模型,因为有未决的更改并且自动迁移已禁用...
我有一个长期有效的解决方案,没有任何问题。实际上,我几天前就对数据库进行了更改,并且使用该标准add-migration
后一切都很好,update-database
并且没有任何问题。
但是,今天我又对数据库进行了更改,add-migration
之后又做了update-database
。但是,当我运行该应用程序时,出现上述错误。
我可以通过在Application_Start中包含以下内容来确保运行迁移:
ConfigurationPlatform configurationPlatform = new ConfigurationPlatform();
DbMigrator migratorPlatform = new DbMigrator(configurationPlatform);
migratorPlatform.Update();
Run Code Online (Sandbox Code Playgroud)
配置类如下所示:
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 …
Run Code Online (Sandbox Code Playgroud)