sql server - EF5 Code First Automatic Migrations: Renaming Primary Key gives error: column names in each table must be unique -
i have entity relations primary key relid. change key relationid
first, changed columname in ssms: sql table
then used (in visual studio) rename change word relid relationid, in references.
i thought change in database , in code, convince code work new named primary key, alas following error arises when run project:
column names in each table must unique. column name 'relationid' in table 'dbo.relations' specified more once.
of course, isn't case. in table column relationid exists once. it's ef5 trying add column same name.
can explain me why error occurs , how 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 class oefenfirmadropcreateandseed (not seeding anymore):
public class oefenfirmadropcreateandseed : migratedatabasetolatestversion<oefenfirmacontext, migrations.configuration> { }
you using add-migrations right? then, need not update on database side manually using manual scripts. should in add-migration scripts alone. if manually run scripts rename column in database side existing system generated migrationscripts , database in out of sync. so, end error while running/rollback new/old migration scripts. per understanding, please try out following steps
step 1: rename primary key of model class (relid relationid) in visual studio code.
step 2: run add-migration renamemycolumn in package manager console. system generate migrationscripts based on model changes below.
note: please verifies system generated addcolumn , dropcolumn instead of renamecolumn scripts below in system generated migration scripts. if yes, please remove addcolumn , dropcolumn scripts manually , add renamecolumn() scripts in up() , down() methods 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 following auto-generated lines(note:these scripts generated example purpose not primary key) addcolumn("dbo.mytable", "newcolumn", c => c.string(nullable: false, maxlength: 50)); dropcolumn("dbo.mytable", "oldcolumn"); // add line renamecolumn("dbo.mytable", "oldcolumn", "newcolumn"); } public override void down() { // remove following auto-generated lines(note:these scripts generated example purpose not primary key) addcolumn("dbo.mytable", "oldcolumn", c => c.string(nullable: false, maxlength: 50)); dropcolumn("dbo.mytable", "newcolumn"); // add line renamecolumn("dbo.mytable", "newcolumn", "oldcolumn"); } } } hope might move forward!
Comments
Post a Comment