asp.net mvc 4 - Update Database command not working in package manager console -


i have made changes in model classes like--- added few new property , drop requirement changes...my old city class have following columns

                  public class city()                   {                     public int cityid { get; set;}                      public string cityname { get; set;}                      public decimal longitude { get; set;}                      public decimal latitude { get; set;}                    } 

now, have made changes in city class.now contain 2 fields---

                 public class city()                 {                  public int id { get; set;}                  public string name { get; set;}                  } 

now, run simple add-migration command in package manager console---

           pm> add-migration ccccxxxx runs 

then tried update-database command in package manager gave me following error---

      pm> update-database       multiple identity columns specified table 'cities'. 1 identity column per table allowed. 

then searched error , came link------ http://www.itorian.com/2013/10/multiple-identity-columns-specified-for.html followed step given there , changes voidup() method accordingly this---

      public partial class ccccxxxx : dbmigration         {           public override void up()            {             dropprimarykey("dbo.cities");             dropcolumn("dbo.cities", "cityid");             dropcolumn("dbo.cities", "cityname");             dropcolumn("dbo.cities", "latitude");             dropcolumn("dbo.cities", "longitude");             addcolumn("dbo.cities", "id", c => c.int(nullable: false, identity: true));             addcolumn("dbo.cities", "name", c => c.string());             addprimarykey("dbo.cities", "id");          }          public override void down()             {             addcolumn("dbo.cities", "longitude", c => c.decimal(nullable: false, precision: 18, scale: 2));             addcolumn("dbo.cities", "latitude", c => c.decimal(nullable: false, precision: 18, scale: 2));             addcolumn("dbo.cities", "cityname", c => c.string());             addcolumn("dbo.cities", "cityid", c => c.int(nullable: false, identity: true));             dropprimarykey("dbo.cities");             dropcolumn("dbo.cities", "name");             dropcolumn("dbo.cities", "id");             addprimarykey("dbo.cities", "cityid");         }     } 

now, getting following error in package manager console.totally frustating message---

 alter table drop column failed because 'longitude' data column in table 'cities'. table must have @ least 1 data column. 

dont know how fix bug--- detailed processing script of package manager console window is---

   pm> update-database    specify '-verbose' flag view sql statements being applied  target database.     applying explicit migrations: [201507211404004_ccccxxxx].     applying explicit migration: 201507211404004_ccccxxxx.      system.data.sqlclient.sqlexception (0x80131904): alter table drop          column failed because 'longitude' data column in table 'cities'.  table must have @ least 1 data column.     @ system.data.sqlclient.sqlconnection.onerror(sqlexception exception, b       boolean breakconnection, action`1 wrapcloseinaction)     @ system.data.sqlclient.sqlinternalconnection.onerror(sqlexception  exception, boolean breakconnection, action`1 wrapcloseinaction)      @ system.data.sqlclient.tdsparser.throwexceptionandwarning(tdsparserstateobject stateobj, boolean callerhasconnectionlock, boolean asyncclose)     @ system.data.sqlclient.tdsparser.tryrun(runbehavior runbehavior, sqlcommand cmdhandler, sqldatareader datastream, bulkcopysimpleresultset bulkcopyhandler, tdsparserstateobject stateobj, boolean& dataready)     @ system.data.sqlclient.sqlcommand.runexecutenonquerytds(string methodname, boolean async, int32 timeout, boolean asyncwrite)     @ system.data.sqlclient.sqlcommand.internalexecutenonquery(taskcompletionsource`1 completion, string methodname, boolean sendtopipe, int32 timeout, boolean asyncwrite)     @ system.data.sqlclient.sqlcommand.executenonquery()     @ system.data.entity.infrastructure.interception.dbcommanddispatcher. <nonquery>b__0(dbcommand t, dbcommandinterceptioncontext`1 c)     @ system.data.entity.infrastructure.interception.internaldispatcher`1.dispatch[ttarget,tinterceptioncontext,tresult](ttarget target, func`3 operation, tinterceptioncontext interceptioncontext, action`3 executing, action`3 executed)     @ system.data.entity.infrastructure.interception.dbcommanddispatcher.nonquery(dbcommand command, dbcommandinterceptioncontext interceptioncontext)    @ system.data.entity.internal.interceptabledbcommand.executenonquery()    @ system.data.entity.migrations.dbmigrator.executesql(migrationstatement migrationstatement, dbconnection connection, dbtransaction transaction, dbinterceptioncontext interceptioncontext)    @ system.data.entity.migrations.infrastructure.migratorloggingdecorator.executesql(migrationstatement migrationstatement, dbconnection connection, dbtransaction transaction, dbinterceptioncontext interceptioncontext)    @ system.data.entity.migrations.dbmigrator.executestatementsinternal(ienumerable`1 migrationstatements, dbconnection connection, dbtransaction transaction, dbinterceptioncontext interceptioncontext)    @ system.data.entity.migrations.dbmigrator.executestatementswithintransaction(ienumerable`1 migrationstatements, dbtransaction transaction, dbinterceptioncontext interceptioncontext)     @ system.data.entity.migrations.dbmigrator.executestatementswithinnewtransaction(ienumerable`1 migrationstatements, dbconnection connection, dbinterceptioncontext interceptioncontext)      @ system.data.entity.migrations.dbmigrator.executestatementsinternal(ienumerable`1 migrationstatements, dbconnection connection, dbinterceptioncontext interceptioncontext)      @ system.data.entity.migrations.dbmigrator.executestatementsinternal(ienumerable`1 migrationstatements, dbconnection connection)      @ system.data.entity.migrations.dbmigrator.<>c__displayclass30.<executestatements>b__2e()      @ system.data.entity.sqlserver.defaultsqlexecutionstrategy.<>c__displayclass1.<execute>b__0()      @ system.data.entity.sqlserver.defaultsqlexecutionstrategy.execute[tresult](func`1 operation)      @ system.data.entity.sqlserver.defaultsqlexecutionstrategy.execute(action operation)      @ system.data.entity.migrations.dbmigrator.executestatements(ienumerable`1 migrationstatements, dbtransaction existingtransaction)      @ system.data.entity.migrations.dbmigrator.executestatements(ienumerable`1 migrationstatements)       @ system.data.entity.migrations.infrastructure.migratorbase.executestatements(ienumerable`1 migrationstatements)      @ system.data.entity.migrations.dbmigrator.executeoperations(string migrationid, versionedmodel targetmodel, ienumerable`1 operations, ienumerable`1 systemoperations, boolean downgrading, boolean auto)       @ system.data.entity.migrations.dbmigrator.applymigration(dbmigration migration, dbmigration lastmigration)       @ system.data.entity.migrations.infrastructure.migratorloggingdecorator.applymigration(dbmigration migration, dbmigration lastmigration)       @ system.data.entity.migrations.dbmigrator.upgrade(ienumerable`1 pendingmigrations, string targetmigrationid, string lastmigrationid)       @ system.data.entity.migrations.infrastructure.migratorloggingdecorator.upgrade(ienumerable`1 pendingmigrations, string targetmigrationid, string lastmigrationid)      @ system.data.entity.migrations.dbmigrator.updateinternal(string targetmigration)      @ system.data.entity.migrations.dbmigrator.<>c__displayclassc.<update>b__b()       @ system.data.entity.migrations.dbmigrator.ensuredatabaseexists(action mustsucceedtokeepdatabase)       @ system.data.entity.migrations.infrastructure.migratorbase.ensuredatabaseexists(action mustsucceedtokeepdatabase)       @ system.data.entity.migrations.dbmigrator.update(string targetmigration)       @ system.data.entity.migrations.infrastructure.migratorbase.update(string targetmigration)      @ system.data.entity.migrations.design.toolingfacade.updaterunner.run()     @ system.appdomain.docallback(crossappdomaindelegate callbackdelegate)     @ system.appdomain.docallback(crossappdomaindelegate callbackdelegate)      @ system.data.entity.migrations.design.toolingfacade.run(baserunner runner)     @ system.data.entity.migrations.design.toolingfacade.update(string targetmigration, boolean force)     @ system.data.entity.migrations.updatedatabasecommand.<>c__displayclass2.<.ctor>b__0()     @ system.data.entity.migrations.migrationsdomaincommand.execute(action command)     clientconnectionid:de580d2c-d99b-4a45-bb88-736c802e5104     alter table drop column failed because 'longitude' data c   column in table 'cities'. table must have @ least 1 data column.     pm>  

change up() code don't drop every field:

      public override void up()        {         addcolumn("dbo.cities", "name", c => c.string());         dropprimarykey("dbo.cities");         dropcolumn("dbo.cities", "cityid");         dropcolumn("dbo.cities", "cityname");         dropcolumn("dbo.cities", "latitude");         dropcolumn("dbo.cities", "longitude");         addcolumn("dbo.cities", "id", c => c.int(nullable: false, identity: true));         addprimarykey("dbo.cities", "id");      } 

and down() code to

    public override void down()         {         dropcolumn("dbo.cities", "id");         addcolumn("dbo.cities", "longitude", c => c.decimal(nullable: false, precision: 18, scale: 2));         addcolumn("dbo.cities", "latitude", c => c.decimal(nullable: false, precision: 18, scale: 2));         addcolumn("dbo.cities", "cityname", c => c.string());         addcolumn("dbo.cities", "cityid", c => c.int(nullable: false, identity: true));         dropprimarykey("dbo.cities");         dropcolumn("dbo.cities", "name");         addprimarykey("dbo.cities", "cityid");     } 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -