Column names in each table must be unique. Column name 'StripeRecipientId' in table 'dbo.Foos' is specified more than once
Asked Answered
B

13

23

I have a model class named Foo that has, among others, these properties.

public string StripeRecipientId { get; set; }

public override bool HasProvidedBillingInformation
{
    get
    {
        // return !string.IsNullOrEmpty(this.StripeRecipientId);

        return false;
    }
}

I have enabled migrations and am using Code First. When I run the update-database commandlet, whether with -Force option is specified or not, I get this error:

Column names in each table must be unique. Column name 'StripeRecipientId' in table 'dbo.Foos' is specified more than once.

I double-checked and triple checked and there's only one column of that name in my model as well as in the table. This column was created already by a previous run of the update-database commandlet just a while ago.

I am tempted to delete my database and then apply the migrations, but that will mean me having to create a lot of test data just to be able to test the feature I am working on just now.

I am using Entity Framework v6.1.2.

How do I get rid of this error?

Bearish answered 29/9, 2015 at 13:31 Comment(4)
Perhaps EF doesn't think is applied the previous migration and is attempting to reapply it? Try "update-database -verbose" to see which migrations it is attempting to applyHeated
I forgot to mention, I always do a -Verbose. It is attempting to recreate that column again and I don't know why.Bearish
Excellent habit :) Is it attempting to apply an old migration, or is your newest migration attempting to create the column?Heated
I've never had much luck with code-first migrations. The Database initialiser seems to do this regularly (causing errors when running the application). I really want to do code-first, particularly from the source control point of view.. but I've not had much luck with it. And with multiple contexts, the commands are ridiculous. Data First seems so much easier in comparison.Lori
B
32

Run the Add-Migration command with the -IgnoreChanges flag. Then run Update-Database again.

-Update-

These commands should be run in the Package Manager Console. From the main menu: Tools-> NuGet Package Manager -> Package Manager Console.

Butadiene answered 11/1, 2016 at 5:18 Comment(7)
Thank you. You're right, though it's been a while I had that problem and it went away and I can't remember. I have only recently made time to study migrations properly though I have been using them for some time now.Bearish
@Butadiene Thanks. I tried to run them in Package Manager actually, but it didn't recognize the commands and returns this error message: Add-Migrations : The term 'Add-Migrations' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. Is there anything else I should do before trying to run these commands on Package Manager console?Metrist
@UlyssesAlves Have you installed Entity Framework and run the Enable-Migrations command already? Keep in mind, SQL could still throw this error for reasons completely unrelated to the Entity Framework migrations table being out-of-sync.Butadiene
@UlyssesAlves It's Add-Migration and not Add-MigrationsMankind
I get an error trying this: "A parameter cannot be found that matches parameter name 'ignorechanges'"Britton
As an alternative approach, I have done this: 1- I've removed the new migration file created by this command which hads error (in Migrations folder) , 2- Undo the ...ContextModelSnapshot.cs to previous version, 3- run Add-Migration 4-script-migration , VS creates a sql script, I copy it and run it on DB as a scriptVeach
How would this command look precisely?Raisaraise
O
3

Add-Migration -IgnoreChanges as it is worked for me, but still threw up more errors.

Overarch answered 14/9, 2018 at 14:30 Comment(0)
O
2

If you have also added these properties in the Database tables you will continue to get these errors. So I had to delete the properties from my own table to achieve a re-scaffolding and updated the database subsequently. So run Add-Migration after you must have removed the properties and you will obtain a satisfactory outcome, then run Update-Database it is also expected to be satisfactory.

Overarch answered 14/9, 2018 at 16:5 Comment(0)
O
1

This worked for me.

  1. I run this Add-Migration -IgnoreChanges and i got error that your previous two migrations are in pending. Example: Migration-1 , Migration-2

  2. I run update-database -target Migration-1 and

    update-database -target Migration-2

  3. Database Successfully created. Running Seed method.

Orthoepy answered 3/1, 2019 at 3:34 Comment(0)
G
1

This works for me in two steps:

Step 1) Just delete this column from a table in Database
Step 2) Rebuild the Project and run Project.
Gorga answered 5/3, 2020 at 5:54 Comment(0)
A
1

Regarding People's issues with getting -IgnoreChanges to work

So for my case what I found the issue to be was that the -IgnoreChanges option is specific to Entity Framework, while I was using EFCore. What I found to be a solution was reviewing the individual commits in the migrations folder of your database project. By following the advice of this link and commenting out the specific Up function body I was able to run Update-Database. Updating the database works.

Aide answered 24/11, 2021 at 4:51 Comment(0)
I
1

In my case, this error came from a previous migration being completed manually and trying to add the column again through entity framework. The error was cleared by simply commenting out the last migration's Add/Drop commands auto-generated through Entity Framework, and then again running the update command for the current migration.

Importunity answered 10/4, 2023 at 22:34 Comment(0)
M
0

Simple delete files in Migration folder and run following command:

add-migration seednew1

And then:

update-database -verbose
Mccallum answered 14/1, 2021 at 9:59 Comment(0)
M
0

For me the database snapshot which I must have messed up during Add/Remove operations was different from what was actually persisted in the database, so I ended up discarding the snapshot changes and re-applying the migration.

Mauceri answered 2/2, 2021 at 12:43 Comment(0)
P
0

the update-database process is referencing the older migrations [which perhaps contain some add columns scripts] ... DELETE the older migrations with these adds... remember to KEEP the SNAPSHOT [summary of the saved scripts] ... and run your migration again... it should work! all the best :))

Phlegmatic answered 11/8, 2021 at 17:36 Comment(0)
Q
0

I had the following case: I first added a column to the database using a query, then I tried to do "update-database" in package manager console. He gave me an error. I deleted from the table the column that was in error and did "update-database" in package manager console vs2019 again and it worked

Qp answered 8/12, 2021 at 5:35 Comment(0)
S
0

This also happens when

Wrong DBContext/Catalog is being used

, Lets say you have two Database and you are running update-database command after adding migration.

1)AuthDB 2)CustomerDB

Now first get the query using verbose command , Then try run this on DB which does not have such column (make sure its not a production database) Then you will see this error is thrown.

In short make sure the correct DbContext was specified if working with multiple.

Spies answered 17/10, 2022 at 21:5 Comment(0)
F
0

What worked for me is that I deleted both migration files; run add-migration, then update-database -verbose.

Friedafriedberg answered 7/7, 2023 at 16:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.