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>
{
}
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.
Comments