c#-4.0 – 实体框架5为表指定的多个标识列.每个表只允许一个标识列

前端之家收集整理的这篇文章主要介绍了c#-4.0 – 实体框架5为表指定的多个标识列.每个表只允许一个标识列前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在创建这个模型作为我的代码第一个实体框架的一部分
public class NewUserRegistration
{
    [Key]
    public int NewUserRegistrationId { get; set; }    
}

在Package Manger Console中使用Update-Database -Verbose -Force命令在更新的这一位期间获取此异常应用自动迁移:201211252223088_A自动迁移.

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,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) 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(IEnumerable
1
migrationStatements) at
System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable1
migrationStatements) at
System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String
migrationId,XDocument targetModel,IEnumerable
1 operations,Boolean
downgrading,Boolean auto) at
System.Data.Entity.Migrations.DbMigrator.AutoMigrate(String
migrationId,XDocument sourceModel,Boolean
downgrading) at
System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.AutoMigrate(String
migrationId,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 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.

显然只有一个Identity Column被指定.那么为什么会这样呢?

当我这样做我也不例外.

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

有什么想法为什么会这样呢?

编辑

我应该说,我正在改变关键的名称.评论说你不能这样做.我怎么可以放弃并重新创建?

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

解决方法

尝试重命名Key列时遇到相同的错误.为了使迁移工作,我不得不重新排序我的脚手架迁移脚本中的操作顺序.

在这里,我确定首先订购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");
    }

希望能帮助你的情况.

原文链接:https://www.f2er.com/csharp/95313.html

猜你在找的C#相关文章