实体框架代码迁移 - 停留在初始迁移 [英] Entity Framework Code Migrations - Stuck on Initial Migration
问题描述
然后,我将一个属性添加到我的一个域对象(string property called TestProperty),并添加了一个映射到我的EntityTypeConfiguration文件(我们现在忽略了约定)。
再次调用添加迁移会产生错误:
无法生成显式迁移,因为以下显式迁移处于待处理状态:[201303262144218_Initial]。在尝试生成新的显式迁移之前应用挂起的显式迁移。
但是调用Update-Database会产生一个sql异常,因为表已经存在:
数据库中已经有一个名为Customer的对象
在我的DbContext的构造函数中,我尝试了不同的更新策略,例如:
Database.SetInitializer< UnitOfWork>(new DropCreateDatabaseAlways< UnitOfWork>());
我缺少一些明显的东西吗?
我在这里尝试了解决方案,但它没有起作用: ASP的自动迁移.NET
谢谢
编辑:更新
过去的关键第一步是创建初始迁移,然后从Up和Down方法中删除生成的代码( http://thedatafarm.com/blog/data-access/using-ef-migrations-with-an-existing-database/ )。
然后我可以更新模型和EF地图,然后运行Add-Migration。这将产生一个具有正确Up和Down代码的迁移。
然后,问题是尝试应用更新。更新数据库会产生错误无法更新数据库以匹配当前模型,因为存在待处理的更改并且自动迁移被禁用...自动迁移将DbMigrationsConfiguration.AutomaticMigrationsEnabled设置为true以启用自动迁移
您可以使用Add-Migration命令将挂起的模型更改写入基于代码的迁移。好的,所以我再次尝试添加迁移,并产生与最后一个完全相同的代码的另一个迁移。
我运行Update-Database并重新获得相同的错误。我尝试更新数据库 - 目标迁移201304080859556_MyMigration -Force,但这会产生指定的目标迁移201304080859556_MyMigration不存在。确保目标迁移是指现有的迁移ID - 它是!
非常沮丧!
我有同样的问题,启用EF迁移的代码优先模型,一个现有的数据库,并且执行以下过程:
- 删除项目中现有的Migrations文件夹,并删除表
__ MigrationHistory
- 从包管理器控制台运行
enable-migrations
命令。 - 运行
add-migration
命令创建初始迁移。 - 删除所有 c code code code code code code code code $ c < c $ c>命令来应用初始迁移到你的数据库。
这不会对现有对象进行任何更改(因为Up()
方法不包含代码),但会将现有数据库标记为已迁移到初始状态。 - 更改您的代码优先模型。
- 运行
add-migration
命令来创建新的迁移。
新迁移的Up()
方法中的代码将仅包含对象模型的更改。 - 运行
update-database
命令将更改应用于数据库。
I have added EF 5 to my project via Nuget and enabled migrations with the "Enable-Migrations" command. I have then called "Add-Migration" to generate the base code for generating the schema.
I then added a property to one of my domain objects (string property called "TestProperty") and added a mapping to my EntityTypeConfiguration file (we're ignoring the conventions at the moment).
Calling "Add-Migration" again produces the error:
Unable to generate an explicit migration because the following explicit migrations are pending: [201303262144218_Initial]. Apply the pending explicit migrations before attempting to generate a new explicit migration.
But calling "Update-Database" produces a sql exception because the tables already exist:
There is already an object named 'Customer' in the database
In my constructor for my DbContext I have tried the different update strategies, e.g.:
Database.SetInitializer<UnitOfWork>(new DropCreateDatabaseAlways<UnitOfWork>());
Am I missing something obvious? I tried the solution here but it didn't work: Automatic Migrations for ASP.NET
Thanks
EDIT: Update The key to getting past the first step is to create the initial migration and then delete the generated code from the Up and Down methods (http://thedatafarm.com/blog/data-access/using-ef-migrations-with-an-existing-database/).
I can then update the model and the EF map and then run Add-Migration. This generates a migration with the correct Up and Down code.
The problem is then trying to apply the update. Update-Database produces the error "Unable to update database to match the current model because there are pending changes and automatic migration is disabled...automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration. You can use the Add-Migration command to write the pending model changes to a code-based migration". Ok, so I try Add-Migration again and it produces another migration with the exact same code as the last one.
I run Update-Database and get the same error again. I try "Update-Database -TargetMigration 201304080859556_MyMigration -Force" but this produces "The specified target migration '201304080859556_MyMigration' does not exist. Ensure that target migration refers to an existing migration id" - It does!
Very frustrating!
I had the same problem enabling EF migrations for a code-first model with an existing database, and the following procedure worked:
- Remove the existing Migrations folder in your project, and DROP the table
__MigrationHistory
from the existing database. - Run the
enable-migrations
command from the Package Manager Console. - Run the
add-migration
command to create an initial migration. - Remove all of the code in
Up()
method for the initial migration. - Run the
update-database
command to apply the initial migration to your database. This doesn't make any changes to existing objects (because theUp()
method contains no code), but it marks the existing database as having been migrated to the initial state. - Make changes to your code-first model.
- Run the
add-migration
command to create a new migration. The code in theUp()
method of the new migration will contain only the changes to your object model. - Run the
update-database
command to apply the changes to your database.
这篇关于实体框架代码迁移 - 停留在初始迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!