实体框架5为表指定了多个标识列。每个表只允许一个标识列 [英] Entity Framework 5 Multiple identity columns specified for table. Only one identity column per table is allowed

查看:552
本文介绍了实体框架5为表指定了多个标识列。每个表只允许一个标识列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating this model as part of my code first entity framework

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

使用 Update-Database -Verbose -Force软件包管理器控制台中的命令我在更新的这一点得到此异常应用自动迁移:201211252223088_AutomaticMigration。

Using the Update-Database -Verbose -Force command in the Package Manger ConsoleI get this exception during the this bit of the update Applying automatic migration: 201211252223088_AutomaticMigration.


ALTER TABLE [dbo]。[NewUserRegistration]添加[NewUserRegistrationId]
[int]非空ID System.Data.SqlClient.SqlException
(0x80131904 ):为表
'NewUserRegistration'指定了多个标识列。每个表只允许一个标识列。 System.Data.SqlClient.SqlConnection.OnError上的
(SqlException异常,
布尔值breakConnection,Action 1 wrapCloseInAction)在
上System.Data.SqlClient.SqlInternalConnection.OnError( SqlException
异常,布尔值breakConnection,在
处的动作
1 wrapCloseInAction)System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject
stateObj,布尔值调用程序HasConnectionLock,布尔值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)at
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource 1
完成,字符串methodName,布尔型sendToPipe,Int32超时,
布尔值asyncWrite)在
处System.Data.SqlClient.SqlCommand.ExecuteNonQuery()在
处System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction
事务,MigrationStatement migrationStatement)在
System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable 处的$ b System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction
transaction,MigrationStatement migrationStatement)在
处> 1
migrationStatements)在
处System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable 1
migrationStatements)在
System.Data。 Entity.Migrations.DbMigrator.ExecuteOperations(String
migrationId,XDocument targetModel,IEnumerable
1操作,布尔
降级,布尔自动)在
System.Data.Entity处。 Migrations.DbMigrator.AutoMigrate(字符串
migrationId,XDocument sourceModel,XDocument targetModel,布尔
d (owngrading)在
处System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.AutoMigrate(String
migrationId,XDocument sourceModel,XDocument targetModel,布尔
降级)在
System.Data处。 Entity.Migrations.DbMigrator.Upgrade(IEnumerable 1
endingMigrations,String targetMigrationId,String lastMigrationId)

at
System.Data.Entity.Migrations.Infrastructure。 MigratorLoggingDecorator.Upgrade(IEnumerable
1
endingMigrations,String targetMigrationId,String lastMigrationId)
在System.Data.Entity.Migrations.DbMigrator.Update(String

处的targetMigration)System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(字符串
targetMigration)处
System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()中的

ClientConnectionId:a39395da-5f2b-48e0-bdac-b48d75a68c68多个$为表 NewUserRegistration指定了b $ b个标识列。每个表只允许一个
标识列。

ALTER TABLE [dbo].[NewUserRegistration] ADD [NewUserRegistrationId] [int] NOT NULL IDENTITY System.Data.SqlClient.SqlException (0x80131904): Multiple identity columns specified for table 'NewUserRegistration'. Only one identity column per table is allowed. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 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) at 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 migrationStatements) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable1 migrationStatements) at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable1 operations, Boolean downgrading, Boolean auto) at System.Data.Entity.Migrations.DbMigrator.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading) at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable1 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.Update(String targetMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() ClientConnectionId:a39395da-5f2b-48e0-bdac-b48d75a68c68 Multiple identity columns specified for table 'NewUserRegistration'. Only one identity column per table is allowed.

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

There is plainly only one Identity Column specified. So why is this the case?

这样做的时候我也不会例外。

When I do this I get no exception.

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

有人对为什么会这样吗?

Any thoughts on why this is the case?

EDIT

EDIT

我应该说我正在更改键。评论说您不能只是这样做。如何删除并重新创建?

I should say that I am changing the name of the key. The comments say that you can't just do this. How can I drop and recreate?

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

Is it best to delete the database from SQL and then just run the Update-Database command again?

推荐答案

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

I encountered the same error when trying to rename a Key column. To make the migration work, I had to reorder the order of operations in my scaffolded migration script.

在这里,我确保先对Drop操作进行排序,然后添加

Here, I made sure to order the Drop operations first, then added the new Key field afterwards.

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

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

Hope that helps with your case.

这篇关于实体框架5为表指定了多个标识列。每个表只允许一个标识列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆