如何从头重新创建迁移 [英] How to recreate migrations from scratch
问题描述
所以我试图回滚到使用EF5创建的迁移。但是我打错了表格上的FOREIGN KEY约束可能导致循环或多个级联路径。我认为这是因为当我正在修复迁移完全相同的问题时,我忽略了修复Down迁移。 (应在此之前阅读此)
无论如何,而不是尝试修复这一切,我试图重置所有迁移 - 如此处。我删除了数据库中的迁移表和所有迁移的.cs文件,然后在包管理器中启用迁移 - 启用自动迁移--Force
和添加迁移初始
当我尝试使用现有的数据库初始化程序(其自动迁移为false)运行我的应用程序时,它失败,因为它试图创建表已经在那里所以我改变了我的初始化器到$ code Database.SetInitializer(新的DropCreateDatabaseAlways< MyContext>())
所以我更改了所有的 cascadeDelete:true
到 cascadeDelete:false
在迁移文件中
但是我还是得到相同的错误!
更新1 除了在迁移文件中创建1表之外,我删除了所有内容,但是我遇到了相同的错误。在某个地方必须有某种缓存,或者它正在拾取一个我不知道的文件,或者在后台生成自己的迁移。
更新2 我认为当使用 DropCreateDatabaseAlways
时,EF必须始终生成迁移,并且将 cascadeDelete
更改为false迁移文件是错误的做法。应该在FluentAPI中完成。所以我把这行代码添加到modelBuilder.Conventions.Remove< OneToManyCascadeDeleteConvention>(); 到onmodelcreating中。而且我也删除了初始迁移文件。然后我运行应用程序并正确生成了一个数据库。我以为我会破解它但是....
我将初始化更改为使用我的原始配置文件:
内部密封类配置:DbMigrationsConfiguration< SID2013Context>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(etc ..
}
然后我运行应用程序,它报告了模型已经改变,所以我添加了迁移更新数据库,创建了一个迁移文件来创建数据库。
$ b $现在的问题是,当我运行应用程序时,它尝试运行另一个更新(即使AutomaticMigrationsEnabled = false),我得到数据库中已经有一个名为xxx的对象,再次出现问题在迁移表中的条目与配置文件的名称不匹配。
我最终通过删除数据库手动运行应用程序与原始的 MigrateDatabaseToLatestVersion
初始化程序。我没有意识到这将创建数据库,如果它不存在,没有必要使用 DropCreateDatabaseAlways
初始化程序,然后更改为 MigrateDatabaseToLatestVersion
I recently upgraded from EF6 alpha 1-3 to EF6 beta 1. This meant that I had to recreate all the migrations created using the alpha version.
So I tried to roll back to a migration created using EF5. But I hit the error Introducing FOREIGN KEY constraint on table may cause cycles or multiple cascade paths. I figure this is because I had neglected to fix Down migrations when I was fixing Up migrations for exactly the same problem. (Should have read this before)
Anyway, rather than try to fix it all up I am trying to reset all the migrations - as described here. I deleted my migrations table in the database and all migration .cs files, then in package manager Enable-Migrations -EnableAutomaticMigrations -Force
and Add-Migration Initial
When I tried to run my application with my existing database initialiser (which has automatic migrations false) it failed because it tried to create tables that were already there. So I changed my initialiser to Database.SetInitializer(new DropCreateDatabaseAlways<MyContext>())
This time I got the Introducing FOREIGN KEY constraint on table may cause cycles or multiple cascade paths problem again during initialisation
So I changed ALL the cascadeDelete: true
to cascadeDelete: false
in the migration file
But I still get the same error!
Update 1 I removed all but the creation of 1 table in the migration file but I got the same error. There must be some kind of cache somewhere or it's picking up a file I don't know about or it's generating its own migration in the background
Update 2 I figured that when using DropCreateDatabaseAlways
that EF must always generate the migrations and also that changing cascadeDelete
to false in the migration file is the wrong place to do it. It should be done in the FluentAPI. So I added this line modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
to onmodelcreating. And I also deleted the Initial migration file. Then I ran the application and it correctly generated a database. I thought I'd cracked it but....
I changed initialisation to use my original configuration file:
internal sealed class Configuration : DbMigrationsConfiguration<SID2013Context>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(etc..
}
Then I ran the application and it reported that the model had changed. So I did Add-Migration Update-Database and a migration file to create the database was created.
The problem now is that when I run the application it tries to run another update (even though AutomaticMigrationsEnabled = false). I get the "There is already an object named 'xxx' in the database" problem again. There is an entry in the migrations table that does not match the name of the configuration file.
I finally fixed this by dropping the database manually then running the application with the original MigrateDatabaseToLatestVersion
initializer. I had not realised that this would create the database if it did not exist and there was no need to use a DropCreateDatabaseAlways
initializer then change to MigrateDatabaseToLatestVersion
这篇关于如何从头重新创建迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!