Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
During code First Migration when PM>Update-Database. Iam getting the above error. How to solve it??
Posted
Updated 8-Oct-20 9:23am
v2
Comments
dharam1 16-Jan-14 5:09am    
Please provide some more info.
ahmed ali mohd 16-Jan-14 5:33am    
iam building an MVC project. i have added the model with a string value Url_Default. Now iam updating it using CodeFirstmigration command PM>Update-Database. It is displaying the above error
♥…ЯҠ…♥ 16-Jan-14 7:58am    
You sure you have only one column named Url_Default? check your class file once again
ahmed ali mohd 16-Jan-14 8:00am    
actually i have 10 columns..i have added url_default..it means i have 11 now
♥…ЯҠ…♥ 16-Jan-14 8:04am    
Could you check your database for the column name, in team someone might updated the database with same column name and once you try to update it you might end up with this error, check your table and bounce back

Error means what it says. You have the same column name twice
 
Share this answer
 
Comments
Maciej Los 16-Jan-14 16:55pm    
Simpler can't be!
I was having this same problem, and it took me a while to figure out what was going on. The problem was that I needed to seed the column I just added with a default value that needed to be retrieved via a function that depended on another entity.

So when I was doing this in the same migration script, when it ran that 2nd step to query against another entity, because the migration execution had not been committed yet by .NET, as soon as I hit the other entity, the same migration script was getting triggered again.

This was causing it to run that same AddColumn() twice, which is what threw the error. I would personally consider this a BUG in code-first migrations, as it requires you to create two migration scripts, to make sure that 1st one is fully completely before the 2nd one runs.

In my opinion, this is not just silly, it introduces a potential migration catastrophe, because in my situation, that column should never be added without seeding it with the data. A column full of NULLs is technically an invalid state. But unless I'm missing something, there is no way to get that default value from a function that depends on another entity during the same transaction, because that would trigger the re-execution of the same migration script.

I suppose I could technically add a condition that checks for the existence of that new column before executing the AddColumn(), but that's just silly as well. The whole point of code-first migrations is to assume that the database schema is in a certain state. It doesn't make much sense to add error-handling to the scripts to make sure they work just in case the schema is something unexpected or--even worse--that one of the migration scripts has already run. That's just very, very wrong.
 
Share this answer
 
Run the Add-Migration command with the -IgnoreChanges flag. Then run Update-Database again.

These commands should be run in the Package Manager Console.
If Add-Migration -IgnoreChanges threw up more errors run this that "your previous two migrations are in pending". Example: Migration-A , Migration-B

Run update-database -target Migration-A and update-database -target Migration-B.
Hope it will work..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900