我找到了一种方法来表明我的数据库是最新的,并且它(毫不奇怪)基于修改__MigrationHistory
表,代码优先迁移用于确定运行时将哪些迁移应用于数据库Update-Database
.
顺便说一句,在研究这个答案时,我发现了一个关于代码优先迁移命令的非常好的参考,可以在以下位置找到:http://dotnet.dzone.com/articles/ef-migrations-command http://dotnet.dzone.com/articles/ef-migrations-command
当 EF 自动从头开始创建数据库时,它始终会将单个条目放入__MigrationHistory
表,该条目将具有 MigrationId(currentDateTime)_InitialCreate
。这代表EF刚刚进行了数据库的初始创建。但是,您的迁移历史记录不会以该 MigrationId 开始,因为您将从其他内容开始。
要“愚弄”代码优先迁移,让其认为您正在进行最新迁移,您需要删除该迁移(currentDateTime)_InitialCreate
条目来自__MigrationHistory
新创建的数据库的表,并插入如果您仍然拥有已应用迁移的旧数据库的话本应存在的内容。
因此,首先删除新生成的数据库中的所有内容__MigrationHistory
桌子。然后,进入包管理器控制台并运行:
PM> Update-Database -Script
从生成的 SQL 脚本中,取出以以下内容开头的所有行:
INSERT INTO [__MigrationHistory]...
然后,运行那些INSERT
新创建的数据库上下文中的语句。检查这些行中的每一行现在都存在于您的__MigrationHistory
桌子。当您下次运行时:
PM> Update-Database
...您应该收到一条消息“没有待处理的基于代码的迁移”。恭喜 - 您已经欺骗了代码优先迁移,让其认为您现在正在进行最新的迁移,并且您可以从此处继续添加新迁移。
我确实认为应该有一些自动化的方法来内置到 EF 代码优先中,但是......也许他们应该添加类似的内容:
PM> Update-Database -MigrationsTableOnly
...它将破坏迁移表中的现有条目,并且只是将新条目插入到项目中定义的每个迁移的迁移历史记录中,但实际上并不尝试运行迁移。呃,好吧。
UPDATE
我找到了一种很好地自动化此操作的方法,即使用自定义初始化程序的 Seed 方法。基本上,种子方法会在创建数据库时删除现有的迁移历史数据,并插入您的迁移历史记录。在我的数据库上下文构造函数中,我注册自定义初始值设定项,如下所示:
public class MyDatabaseContext : DbContext {
public MyDatabaseContext() : base() {
Database.SetInitializer(new MyDatabaseContextMigrationHistoryInitializer());
}
自定义初始化程序本身如下所示:
/// <summary>
/// This initializer clears the __MigrationHistory table contents created by EF code-first when it first
/// generates the database, and inserts all the migration history entries for migrations that have been
/// created in this project, indicating to EF code-first data migrations that the database is
/// "up-to-date" and that no migrations need to be run when "Update-Database" is run, because we're
/// already at the latest schema by virtue of the fact that the database has just been created from
/// scratch using the latest schema.
///
/// The Seed method needs to be updated each time a new migration is added with "Add-Migration". In
/// the package manager console, run "Update-Database -Script", and in the SQL script which is generated,
/// find the INSERT statement that inserts the row for that new migration into the __MigrationHistory
/// table. Add that INSERT statement as a new "ExecuteSqlCommand" to the end of the Seed method.
/// </summary>
public class MyDatabaseContextMigrationHistoryInitializer : CreateDatabaseIfNotExists<MyDatabaseContext> {
/// <summary>
/// Sets up this context's migration history with the entries for all migrations that have been created in this project.
/// </summary>
/// <param name="context">The context of the database in which the seed code is to be executed.</param>
protected override void Seed(MyDatabaseContext context) {
// Delete existing content from migration history table, and insert our project's migrations
context.Database.ExecuteSqlCommand("DELETE FROM __MigrationHistory");
context.Database.ExecuteSqlCommand("INSERT INTO __MigrationHistory (MigrationId, Model, ProductVersion) VALUES ('201210091606260_InitialCreate', 0x1F8B0800000000000400ECBD07601C499625262F6DCA7B7F4AF54AD7E074A..., '5.0.0.net40')");
context.Database.ExecuteSqlCommand("INSERT INTO __MigrationHistory (MigrationId, Model, ProductVersion) VALUES ('201210102218467_MakeConceptUserIdNullable', 0x1F8B0800000000000400ECBD07601C499625262F6DCA7B7F4..., '5.0.0.net40')");
context.Database.ExecuteSqlCommand("INSERT INTO __MigrationHistory (MigrationId, Model, ProductVersion) VALUES ('201210231418163_ChangeDateTimesToDateTimeOffsets', 0x1F8B0800000000000400ECBD07601C499625262F6D..., '5.0.0.net40')");
context.Database.ExecuteSqlCommand("INSERT INTO __MigrationHistory (MigrationId, Model, ProductVersion) VALUES ('201210251833252_AddConfigSettings', 0x1F8B0800000000000400ECBD07601C499625262F6DCA7B7F4AF54AD7E..., '5.0.0.net40')");
context.Database.ExecuteSqlCommand("INSERT INTO __MigrationHistory (MigrationId, Model, ProductVersion) VALUES ('201210260822485_RenamingOfSomeEntities', 0x1F8B0800000000000400ECBD07601C499625262F6DCA7B7F4AF5..., '5.0.0.net40')");
}
}