EF Applies All Migrations, Not Just New One

2019-09-15 19:59发布

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.

1条回答
啃猪蹄的小仙女
2楼-- · 2019-09-15 20:29

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)

查看更多
登录 后发表回答