Scarnet
Scarnet

Reputation: 500

Column name is specified more than once EF Code First Migration

I have two columns in a table which I created them using code first approach then I changed their data types in the C# model not the actual database and last I tried the later migration in order to change their data type in the database

public partial class PriceAndMargin : DbMigration
{
    public override void Up()
    {
        AddColumn("dbo.Items", "Price", c => c.Int(nullable: false));
        AddColumn("dbo.Items", "ShortageMargin", c => c.Int(nullable: false));
    }
    
    public override void Down()
    {
        DropColumn("dbo.Items", "ShortageMargin");
        DropColumn("dbo.Items", "Price");
    }
}

If I got it right, this code should drop the Price column and the ShortageMargin column and recreates them but instead it displays this message in the console for me

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

thanks in advance

Upvotes: 0

Views: 5433

Answers (1)

Basque
Basque

Reputation: 128

If I got it write this code should drop the Price column and the ShortageMargin column and recreates them

Not really, your "up" adds the columns. The "down" section is only if you want to "downgrade" your database (ou revert the migration). I don't know how you changed the type of your object, and from which version you ran the 'add-migration' command but you should have a script with "AlterColumn instead of AddColumn

Upvotes: 3

Related Questions