【发布时间】:2016-04-26 10:34:58
【问题描述】:
我正在尝试创建一个服务调用,它将 EF 代码优先数据库更新到最新版本。但是,它总是尝试(并且显然失败)执行所有迁移,无论它们是否已经执行。
这是我的代码:
var config = new DbMigrationsConfiguration<MyContext>();
config.MigrationsAssembly = typeof (MyContext).Assembly;
config.MigrationsNamespace = "Context.Migrations";
//This had no effect
//config.SetHistoryContextFactory(connectionString.ProviderName, (connection, s) => new HistoryContext(context.Database.Connection, "dbo"));
var migrator = new DbMigrator(config);
//This gets every single migration, even the ones already executed before
var migrations = migrator.GetPendingMigrations();
//This gets 0 migrations
var existing = migrator.GetDatabaseMigrations();
//This gets all migrations
var other = migrator.GetLocalMigrations();
//This returns true! So it knows all the migrations are already executed
var differ = context.Database.CompatibleWithModel(true);
//This will throw an exception because it's trying to create existing tables!
migrator.Update();
我可以确认迁移历史记录表包含 [dbo]。[__MigrationHistory] 引用了所有旧迁移。
我检查了迁移器中的连接字符串。我还尝试手动设置历史上下文工厂,以防它在其他地方寻找没有结果。此外,直接从控制台运行 update-database 可以正常工作,并且没有挂起的更新。
感谢任何帮助
【问题讨论】:
标签: entity-framework entity-framework-migrations