【问题标题】:Entity Framework 5 Multiple identity columns specified for table. Only one identity column per table is allowed实体框架 5 为表指定了多个标识列。每个表只允许一个标识列
【发布时间】:2012-11-25 22:28:50
【问题描述】:

我正在创建这个模型作为我的代码优先实体框架的一部分

public class NewUserRegistration
{
    [Key]
    public int NewUserRegistrationId { get; set; }    
}

在包管理器控制台中使用Update-Database -Verbose -Force 命令我在更新Applying automatic migration: 201211252223088_AutomaticMigration. 期间遇到此异常

ALTER TABLE [dbo].[NewUserRegistration] 添加 [NewUserRegistrationId] [int] NOT NULL IDENTITY System.Data.SqlClient.SqlException (0x80131904): 为表指定了多个标识列 '新用户注册'。每个表只允许一个标识列。 在 System.Data.SqlClient.SqlConnection.OnError(SqlException 异常, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler、SqlDataReader 数据流、 BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean & dataReady) 在 System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(字符串 方法名,布尔异步,Int32 超时)在 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable1 迁移声明)在 System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable1 migrationStatements) at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable1 操作,布尔 降级,布尔自动)在 System.Data.Entity.Migrations.DbMigrator.AutoMigrate(字符串 migrationId, XDocument sourceModel, XDocument targetModel, Boolean 降级)在 System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.AutoMigrate(字符串 migrationId, XDocument sourceModel, XDocument targetModel, Boolean 降级)在 System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable
1 pendingMigrations, 字符串 targetMigrationId, 字符串 lastMigrationId)
在 System.Data.Entity.Migrations.DbMigrator.Update(字符串 目标迁移)在 System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(字符串 目标迁移)在 System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() 在 System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() ClientConnectionId:a39395da-5f2b-48e0-bdac-b48d75a68c68 多个 为表“NewUserRegistration”指定的标识列。只有一个 允许每个表的标识列。

显然只指定了一个标识列。那为什么会这样呢?

当我这样做时,我没有例外。

public class NewUserRegistration
{
    [Key]
    public int Id { get; set; }    
}

对为什么会这样有什么想法吗?

编辑

我应该说我正在更改密钥的名称。 cmets说你不能这样做。如何删除和重新创建?

最好从 SQL 中删除数据库,然后再次运行 Update-Database 命令?

【问题讨论】:

  • 看起来您正在更改现有表。在添加NewUserRegistrationId 之前,您在实体中是否还有其他键?不能简单地更改标识列。
  • 是的,那么我如何指示事物删除并重新创建表。

标签: c#-4.0 entity-framework-5


【解决方案1】:

我在尝试重命名键列时遇到了同样的错误。为了使迁移工作,我必须在我的脚手架迁移脚本中重新排序操作顺序。

在这里,我确保先订购 Drop 操作,然后添加新的 Key 字段。

public partial class RenameKey : DbMigration
{
    public override void Up()
    {
        DropPrimaryKey("dbo.GameSummary", new[] { "OldId" });
        DropColumn("dbo.GameSummary", "OldId");
        AddColumn("dbo.GameSummary", "Id", c => c.Int(nullable: false, identity: true));
        AddPrimaryKey("dbo.GameSummary", "Id");
    }

希望对您的案件有所帮助。

【讨论】:

  • 有人可能认为 DropPrimaryKey 就足够了。重新排序代码时,确保 DropPrimaryKey 和 DropColumn 都发生在 AddColumn 之前,使用 ColumnBuilder 类型方法,其中包括标识:true。
  • 但这不适用于 OldId 列中的现有数据,对吧?
  • 奇怪的是 EF 没有预料到这一点,你必须手动这样做
  • 我在 2014 年 3 月 13 日提供了一个答案,以避免丢失现有数据。
【解决方案2】:

我也没有任何问题,只需用RenameColumn 命令替换相关的DropPrimaryKeyDropColumnAddColumnAddPrimaryKey 命令,例如

public partial class RenameKey : DbMigration
{
    public override void Up()
    {    
        RenameColumn("dbo.GameSummary", "OldId", "Id");
    }
}

【讨论】:

  • 很好的答案。可能是处理现有数据的一种优雅方式。
【解决方案3】:

我在第一次迁移后也遇到了类似的问题。我意识到,在我删除了第一次迁移创建的数据库,然后也删除了在我的 mvc 应用程序中创建的迁移文件夹后,问题没有再次出现。

【讨论】:

    【解决方案4】:

    首先Add-Migration -Name 那么

    public override void Up()
    {    
        RenameColumn("dbo.Department", "OldId", "NewId");
    }
    

    【讨论】:

      【解决方案5】:

      您可以直接从类中使用类似这样的方式更改列的名称:

      [Column("ProductID")]
      

      例子:

      namespace Z_Market.Models
      {
          public  class Product
          {
              [Key, Column("ProductID")]  //This change the name of the column when you are using migration.  If you have a form created already, you have to change the connection in the for to aim the new column name.  
              public int ID { get; set; }
              public string Description { get; set; }
              public decimal Price { get; set; }
              public DateTime LastBuy { get; set; }
              public float Stock { get; set; }
              public string remarks { get; set; }
              public string deleteme { get; set; }
      
              public ICollection<SupplierProduct> SupplierProducts { get; set; }
          }
      }
      

      【讨论】:

      • 这是 EF5 还是 EF6 的答案?
      【解决方案6】:

      最好的实现是:

      /*Replace*/
      DropPrimaryKey("dbo.GameSummary", new[] { "OldId" });
      DropColumn("dbo.GameSummary", "OldId");
      AddColumn("dbo.GameSummary", "Id", c => c.Int(nullable: false, identity: true));
      AddPrimaryKey("dbo.GameSummary", "Id");
      /*For*/
      RenameColumn("dbo.GameSummary", "OldId", "Id");
      

      【讨论】:

        猜你喜欢
        • 2021-09-13
        • 2015-04-15
        • 1970-01-01
        • 2017-01-06
        • 1970-01-01
        • 1970-01-01
        • 2016-10-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多