我首先使用Entity Framework 5.0数据迁移以及代码。 当我在模型中添加新字段并在包管理器控制台中执行以下命令时。
I am using Entity Framework 5.0 Data migrations along with code first. When i add a new field to my model and execute the following command in the package manager console.
"Add-migration AddedField"我所得到的只是一个名为 n_AddedField的空迁移,上下方法
All I get is an empty migration called "n_AddedField", the up and down methods contain no logic.
我尝试了很多事情,重新安装EF nuget软件包,清理我的解决方案,重建,手动删除所有生成的文件和目录。
I tried a bunch of things, reinstalling the EF nuget package, cleaning my solution, rebuilding, manually removing all generated files and directories.
然后我决定放弃所有迁移并重新开始,然后变得很奇怪。 删除所有迁移和数据库中的migrationhistory表后,我使用CreateDatabaseIfNotExists初始化程序重新创建了数据库。完成此操作后,我应该能够创建一个新的初始迁移。但是,当我尝试创建一个新的迁移时,出现一个错误,指出有待处理的迁移,并列出了我刚刚从项目中删除的所有迁移。
Then i decided that i would scrap all my migrations and start over, and then it got weird. After deleting all my migrations, and the migrationhistory table in the database, i recreated the database using the CreateDatabaseIfNotExists initializer. After doing this, I should be able to create a new initial migration. But when i try to create create a new migration, I get an error saying that there are pending migrations, and lists all the migrations that i just deleted from my project.
我不知道为什么EF以及EF如何仍对这些迁移有任何回忆。 我什至尝试搜索文件内容,查看迁移是否保存在其他地方。但是什么都没有。
I have no idea why and how EF still has any recollection of those migrations. I even tried searching through filecontents looking if the migrations were saved somewhere else or something. But nothing..
当scott hansleman演示的舞台上进行数据迁移时,它看起来确实很整洁,但是对于实际工作,我开始寻找替代方案。
Data migrations look really neat when scott hansleman demo's it on stage, but for real work, I'm starting to look for alternatives.
当项目开始时,我们使用的是EF 4.x,不久前切换到5.0,但是自从切换以来,我成功地添加了很多迁移。
When the project started, we were using EF 4.x and a while back switcted to 5.0, but since the switch i have added a bunch of migrations successfully.
有人知道如何解决此问题吗? 基本上,我只是想能够添加迁移并使用更改生成SQL脚本。
Does anyone have any idea how to solve this problem? Basically i just want to be able to add migrations, and generate a sql script with the changes.
推荐答案I遇到类似的问题,即没有找到新的迁移,因此无论我做什么,更新数据库都会给我以下错误:
I had a similar problem where a new migration was not being found, and so update-database was giving me the following error no matter what I did:
Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable 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.进行批处理解决了我的问题,表明EF使用的是旧的/无效的程序集
Doing a "batch clean" solved my problem, suggesting EF was using an old/invalid assembly from a folder other than the currently selected 'solution configuration (e.g. DEBUG)'.
要进行批清理:
关闭对话框,重建并重新尝试迁移。
Close dialog, rebuild and re-attempt migration.
希望这可以帮助其他人。
Hope this helps someone else out there.
更多推荐
EF数据迁移在添加新迁移时不会检测到更改
发布评论