EF应用所有迁移,而不仅仅是新的 [英] EF Applies All Migrations, Not Just New One

查看:170
本文介绍了EF应用所有迁移,而不仅仅是新的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是EF 6,当我运行 Update-Database 它试图应用自项目开始以来的所有迁移,即使只有迁移是新的。



如果我尝试创建一个空的迁移,我收到一个错误,表示我无法创建新的迁移,因为有明确的迁移挂起。 p>

我发现有一个类似的问题的博客帖子建议删除 __ MigrationHistory 表,并执行更新-Database -Script ,剥离所有实际的数据库更改,但在创建__MigrationHistory 迁移插入。我没有放下桌子,但是我没有重命名,这并没有解决我的问题。



这个问题看起来像一个重复的,但是当我运行脚本时,我看到我的上下文键没有改变,所以这不是我的问题。



有什么奇怪的是我向我的模型添加了一个列,然后做了一个添加迁移,没有问题。然后我去吃午餐(但没有做一个更新数据库),然后午饭后我决定添加另一列到我的模型,当我做了 add-migration -force 再次出现此错误。我已经完全回滚了我的工作区(完全删除了工作区,并从机器中删除了所有的代码),但是问题仍然存在。



我在一个团队环境中,但我们每个人都使用我们自己的开发数据库,​​我已经验证了我的Migrations App.Config文件中的连接字符串是正确的。 / p>

我在 Update-Database -Script 命令中运行SQL Profiler,并且在给出之前运行以下查询24次(12批次2):

  IF db_id(N'ISEPDBContexts.AdministratorDb')IS NOT NULL SELECT 1 ELSE SELECT Count (*)FROM sys.databases WHERE [name] = N'ISEPDBContexts.AdministratorDb'

哪个是奇怪的因为这是我的连接字符串的名称,而不是数据库的名称。

解决方案

原来,问题是包管理器控制台没有看到我的配置文件,所以快速搜索,我发现你不仅必须在PMC(我正在做的)中设置默认项目,而且解决方案必须有与Startup Project相同的项目。 (信用


I'm using EF 6 and when I run Update-Database it is trying to apply every migration since the beginning of the project, even though there is only migration that is new.

If I attempt to create an empty migration, I get an error that says I can't create a new migration because there are explicit migrations pending.

The only blog post I found with a similar issue suggested dropping the __MigrationHistory table and doing Update-Database -Script, stripping out all the actual DB changes but leaving in the Create __MigrationHistory and Migration Inserts. I didn't drop the table, but I did rename it, and this didn't solve the problem for me.

This question looks like a duplicate, but when I run the script I see that my context key has not changed, so that's not my problem.

What's weird is I added a column to my model, and then did an add-migration with no problem. Then I went to lunch (but without doing an update-database), then after lunch I decided to add another column to my model and when I did add-migration -force again this error started. I've completely rolled back my workspace (completely delete the workspace and removed all code from my machine), but the problem persists.

I am in a team environment, but we each use our own dev databases, and I've verified the connection string is correct in my Migrations App.Config file.

I ran SQL Profiler during the Update-Database -Script command, and it ran the following query 24 times before giving up (12 batches of 2) :

IF db_id(N'ISEPDBContexts.AdministratorDb') IS NOT NULL SELECT 1 ELSE SELECT Count(*) FROM sys.databases WHERE [name]=N'ISEPDBContexts.AdministratorDb'

Which is weird because that's the name of my connection string, not the name of the database.

解决方案

Turns out the problem was that Package Manager Console wasn't looking at my config file, so a quick search and I found that you have to not only set the Default Project in PMC (which I was doing), but the solution has to have the same project selected as Startup Project. (Credit)

这篇关于EF应用所有迁移,而不仅仅是新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆