简体   繁体   中英

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

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?

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.

Add-Migration -IgnoreChanges因为它对我有用,但仍然抛出了更多错误。

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.

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.

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.

Simple delete files in Migration folder and run following command:

add-migration seednew1

And then:

update-database -verbose

对我来说,在添加/删除操作期间我必须搞砸的数据库快照与实际保存在数据库中的不同,所以我最终放弃了快照更改并重新应用迁移。

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 :))

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM