DbMigrator 尝试执行每个迁移,而不是检测已执行的迁移
DbMigrator trying to execute every migration, not detecting executed migrations
我正在尝试创建一个服务调用,将 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();
我可以确认迁移历史 table 包含 [dbo]。[__MigrationHistory] 引用所有旧迁移。
我检查了迁移器中的连接字符串。我还尝试手动设置历史上下文工厂,以防它在其他地方查找但没有结果。此外,运行 update-database 直接从控制台工作并表示没有待处理的更新。
感谢任何帮助
如果您的数据库已全部更新,并且您不需要旧的迁移表,那么我建议您删除迁移文件夹并使用
重新创建它
enable-migrations
当迁移出现问题时,我总是这样做。我从不需要历史迁移——一旦他们完成了他们的工作——他们就完成了!如果您正在查看配置,请务必保存您的种子方法。你可以看看我之前的post,也许这也会有所帮助。
Migrations
祝你好运。
确保 DbMigrationsConfiguration.ContextKey 设置正确。
此行后的默认值
var config = new DbMigrationsConfiguration<MyContext>();
会这样
"System.Data.Entity.Migrations.DbMigrationsConfiguration`1[MyContextNamespace.MyContext]"
您需要添加以下行
config.ContextKey = "{Your_Context_Key}";
如果您不知道上下文键,请打开您的 [dbo].[__MigrationHistory]
table 并查看 ContextKey
列。
正确设置后,如果您的数据库是最新的,migrator.GetPendingMigrations()
应该 return 为空,migrator.GetDatabaseMigrations()
应该 return 所有迁移。
我正在尝试创建一个服务调用,将 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();
我可以确认迁移历史 table 包含 [dbo]。[__MigrationHistory] 引用所有旧迁移。
我检查了迁移器中的连接字符串。我还尝试手动设置历史上下文工厂,以防它在其他地方查找但没有结果。此外,运行 update-database 直接从控制台工作并表示没有待处理的更新。
感谢任何帮助
如果您的数据库已全部更新,并且您不需要旧的迁移表,那么我建议您删除迁移文件夹并使用
重新创建它 enable-migrations
当迁移出现问题时,我总是这样做。我从不需要历史迁移——一旦他们完成了他们的工作——他们就完成了!如果您正在查看配置,请务必保存您的种子方法。你可以看看我之前的post,也许这也会有所帮助。
Migrations
祝你好运。
确保 DbMigrationsConfiguration.ContextKey 设置正确。
此行后的默认值
var config = new DbMigrationsConfiguration<MyContext>();
会这样
"System.Data.Entity.Migrations.DbMigrationsConfiguration`1[MyContextNamespace.MyContext]"
您需要添加以下行
config.ContextKey = "{Your_Context_Key}";
如果您不知道上下文键,请打开您的 [dbo].[__MigrationHistory]
table 并查看 ContextKey
列。
正确设置后,如果您的数据库是最新的,migrator.GetPendingMigrations()
应该 return 为空,migrator.GetDatabaseMigrations()
应该 return 所有迁移。