Update Database命令在包管理器控制台中不起作用

时间:2015-07-21 18:38:37

标签: asp.net-mvc-4 entity-framework-6 ef-migrations

我在我的模型类中进行了一些更改,比如---添加了一些新属性,并根据需求的变化删除了一些...我的旧城市类有以下列

                  public class City()
                  {
                    public int CityId { get; set;} 
                    public string CityName { get; set;} 
                    public decimal Longitude { get; set;} 
                    public decimal Latitude { get; set;} 
                  }

现在,我在City类中进行了一些更改。现在它只包含两个字段---

                 public class City()
                {
                 public int Id { get; set;}
                 public string Name { get; set;}
                 }

现在,我在包管理器控制台中运行这个简单的Add-Migration命令---

           PM> Add-Migration ccccxxxx
It runs successfully

然后我在包管理器中尝试了Update-Database命令,它给了我以下错误---

      PM> Update-Database
      Multiple identity columns specified for table 'Cities'. Only one identity column per table is allowed.

然后我搜索错误并来到这个链接------     http://www.itorian.com/2013/10/multiple-identity-columns-specified-for.html     我按照那里给出的步骤改变了我的VoidUp()方法,就像这样---

      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");
        }
    }

现在,我在包管理器控制台中收到以下错误。令人沮丧的消息---

 ALTER TABLE DROP COLUMN failed because 'Longitude' is the only data column in table 'Cities'. A table must have at least one data column.

不知道如何修复这个bug --- 包管理器控制台窗口的详细处理脚本是---

   PM> Update-Database
   Specify the '-Verbose' flag to view the SQL statements being applied to  the target database.
    Applying explicit migrations: [201507211404004_ccccxxxx].
    Applying explicit migration: 201507211404004_ccccxxxx.
     System.Data.SqlClient.SqlException (0x80131904): ALTER TABLE DROP          COLUMN failed because 'Longitude' is the only data column in table 'Cities'. A  table must have at least one data column.
    at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, B       Boolean breakConnection, Action`1 wrapCloseInAction)
    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException  exception, Boolean breakConnection, Action`1 wrapCloseInAction)
     at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
    at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
    at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
    at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
    at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
    at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher. <NonQuery>b__0(DbCommand t, DbCommandInterceptionContext`1 c)
    at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
    at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext)
   at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()
   at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(MigrationStatement migrationStatement, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(MigrationStatement migrationStatement, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinTransaction(IEnumerable`1 migrationStatements, DbTransaction transaction, DbInterceptionContext interceptionContext)
    at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinNewTransaction(IEnumerable`1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection)
     at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClass30.<ExecuteStatements>b__2e()
     at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
     at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
     at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements, DbTransaction existingTransaction)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements)
      at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel, IEnumerable`1 operations, IEnumerable`1 systemOperations, Boolean downgrading, Boolean auto)
      at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
      at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
      at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
      at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
     at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
     at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
      at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
      at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
      at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
      at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
     at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
    at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
    at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
     at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
    at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
    at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
    at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
    ClientConnectionId:de580d2c-d99b-4a45-bb88-736c802e5104
    ALTER TABLE DROP COLUMN failed because 'Longitude' is the only data c   column in table 'Cities'. A table must have at least one data column.
    PM> 

1 个答案:

答案 0 :(得分:0)

将您的Up()代码更改为此代码,这样您就不会删除每个字段:

      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");

    }

你的Down()代码

    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");
    }