EF5 Code First Automatic Migrations: Renaming Primary Key gives error: column names in each table must be unique

SergeP

I have an entity Relations with primary key RelId. I would like to change this key to RelationId

First, I changed the Columname in SSMS: SQL Table

Then I used (in Visual Studio) Rename to change the word RelId to RelationId, in all references.

I thought that this change in the database and in the code, would convince my code to work with the new named Primary Key, but alas following error arises when I run the Project:

column names in each table must be unique. Column name 'RelationId' in table 'dbo.Relations' is specified more than once.

Of course, this isn't the case. In the table the column RelationId exists only once. It's like EF5 is trying to add another column with the same name.

Can anyone explain me why this error occurs and how to fix it?

Global.asax

protected void Application_Start()
    {

        //Database.SetInitializer<OefenfirmaContext>(new OefenfirmaDropCreateAndSeed());
        Database.SetInitializer<OefenfirmaContext>(new OefenfirmaDropCreateAndSeed());

        ViewEngines.Engines.Clear();
        ViewEngines.Engines.Add(new RazorViewEngine());

        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);

        ModelBinders.Binders.Add(typeof(ShoppingCart), new CartModelBinder());
    }

and the class OefenfirmaDropCreateAndSeed (not seeding anymore):

public class OefenfirmaDropCreateAndSeed : 
    MigrateDatabaseToLatestVersion<OefenfirmaContext, Migrations.Configuration>
{
}
Vignesh

You are using Add-Migrations right? then, you need not update on the database side manually using manual scripts. You should do it in the Add-Migration scripts alone. If you manually run the scripts for rename column in the database side then Existing system generated MigrationScripts and the Database will be in out of sync. So, You will end up with an error while running/rollback the new/old migration scripts. As per my understanding, please try out the following steps

Step 1: Rename the Primary key of the Model Class (RelId to RelationId) in Visual Studio Code.

Step 2: Run the Add-Migration RenameMyColumn in Package Manager Console. The system will generate the Migrationscripts based on the model changes like below.

Note: Please verifies system generated AddColumn and DropColumn instead of RenameColumn scripts like below in the system generated migration scripts. If yes, Please remove the AddColumn and DropColumn scripts manually and add the RenameColumn() scripts in the up() and down() methods for the renamed Primary key property.

namespace MyProject.Model.Migrations
{
   using System;
   using System.Data.Entity.Migrations;

   public partial class RenameMyColumn : DbMigration
   {
     public override void Up()
     {
        // Remove the following auto-generated lines(Note:These scripts generated for an example purpose not for primary key)
        AddColumn("dbo.MyTable", "NewColumn", c => c.String(nullable: false, maxLength: 50));
        DropColumn("dbo.MyTable", "OldColumn");

        // Add this line
        RenameColumn("dbo.MyTable", "OldColumn", "NewColumn");
    }

    public override void Down()
    {
        // Remove the following auto-generated lines(Note:These scripts generated for an example purpose not for primary key)
        AddColumn("dbo.MyTable", "OldColumn", c => c.String(nullable: false, maxLength: 50));
        DropColumn("dbo.MyTable", "NewColumn");

        // Add this line
        RenameColumn("dbo.MyTable", "NewColumn", "OldColumn");
    }
  }
}

Hope this might help you to move forward!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PRIMARY KEY must be unique error (Django)

From Dev

Column names in each view or function must be unique

From Dev

EF6: Renaming namespace using Code First Migrations

From Dev

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

From Dev

unable to set other specified column as primary key instead of Id column to table using entity framework code first

From Dev

EF 6.1 Code First - the number of columns specified must match the number of primary key columns

From Dev

Can I use Code First Migrations without pluarized table names?

From Dev

get column names of table vs determine column is primary key or not (sql)

From Dev

EF Code First - mapping two foreign key columns in child table to the same primary key

From Dev

PostgreSQL renaming table sequences and primary key

From Dev

Renaming foreign key properties in code first EF classes causes an exception

From Dev

Unique Key Error in Entity Framework Code First

From Dev

Create a column with primary key using migrations

From Dev

Create a column with primary key using migrations

From Dev

EF Code First: Add row to table with a non-identity primary key

From Dev

How to define a table that its primary key is constructed from 2 foreign keys with EF code-first

From Dev

Identity 2.0 code first automatic migrations

From Dev

EF Code First Migrations creating extra foreign key

From Dev

EF5 Migrations throwing error on simplemembership seed

From Dev

Although there is a Composite Primary Key, Hibernate gives an error when only one key is unique

From Dev

sqlite constraint exception primary key must be unique

From Dev

sqlite constraint exception primary key must be unique

From Dev

How to exclude one table from automatic code first migrations in the Entity Framework?

From Dev

ASP .NET C# MVC 5 Code First Foreign Key Property/Column Names

From Dev

MySQL: Renaming primary key

From Dev

android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19)

From Dev

MySQL alter table add column with primary key syntax error

From Dev

Remove table name prefix in EF code first foreign key table column

From Dev

EF Code First - Primary Key Data Type Change

Related Related

  1. 1

    PRIMARY KEY must be unique error (Django)

  2. 2

    Column names in each view or function must be unique

  3. 3

    EF6: Renaming namespace using Code First Migrations

  4. 4

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

  5. 5

    unable to set other specified column as primary key instead of Id column to table using entity framework code first

  6. 6

    EF 6.1 Code First - the number of columns specified must match the number of primary key columns

  7. 7

    Can I use Code First Migrations without pluarized table names?

  8. 8

    get column names of table vs determine column is primary key or not (sql)

  9. 9

    EF Code First - mapping two foreign key columns in child table to the same primary key

  10. 10

    PostgreSQL renaming table sequences and primary key

  11. 11

    Renaming foreign key properties in code first EF classes causes an exception

  12. 12

    Unique Key Error in Entity Framework Code First

  13. 13

    Create a column with primary key using migrations

  14. 14

    Create a column with primary key using migrations

  15. 15

    EF Code First: Add row to table with a non-identity primary key

  16. 16

    How to define a table that its primary key is constructed from 2 foreign keys with EF code-first

  17. 17

    Identity 2.0 code first automatic migrations

  18. 18

    EF Code First Migrations creating extra foreign key

  19. 19

    EF5 Migrations throwing error on simplemembership seed

  20. 20

    Although there is a Composite Primary Key, Hibernate gives an error when only one key is unique

  21. 21

    sqlite constraint exception primary key must be unique

  22. 22

    sqlite constraint exception primary key must be unique

  23. 23

    How to exclude one table from automatic code first migrations in the Entity Framework?

  24. 24

    ASP .NET C# MVC 5 Code First Foreign Key Property/Column Names

  25. 25

    MySQL: Renaming primary key

  26. 26

    android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19)

  27. 27

    MySQL alter table add column with primary key syntax error

  28. 28

    Remove table name prefix in EF code first foreign key table column

  29. 29

    EF Code First - Primary Key Data Type Change

HotTag

Archive