如您所见,我正在创建一个包含一个主键的新类,我将数据库重新设置为重新创建它,但
public class CustomerContracts
{
[Key, Column("ItemId")] //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 ItemId { get; set; }
public string Description { get; set; }
public string CustomerRef { get; set; }
public int ContractTypeId { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public bool isActive { get; set; }
[DecimalPrecision(18, 2)]
public decimal Price { get; set; }
}
但是当我尝试更新数据库时,我收到以下错误。
指定'-Verbose'标志以查看应用于目标数据库的SQL语句。 应用显式迁移:[201804260855058_firstmigration]。 应用显式迁移:201804260855058_firstmigration。 System.Data.SqlClient.SqlException(0x80131904):为表'CustomerContracts'指定了多个标识列。每个表只允许一个标识列。 在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,Boolean breakConnection,Action
1 wrapCloseInAction) at 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&amp; dataReady) 在System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName,Boolean async,Int32 timeout,Boolean asyncWrite) 在System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext
1 c) 在System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func
3操作,TInterceptionContext interceptionContext,Action3 executing, Action
3执行) 在System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand命令,DbCommandInterceptionContext interceptionContext) 在System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery() 在System.Data.Entity.Migrations.DbMigrator.ExecuteSql(MigrationStatement migrationStatement,DbConnection连接,DbTransaction事务,DbInterceptionContext interceptionContext) 在System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(MigrationStatement migrationStatement,DbConnection连接,DbTransaction事务,DbInterceptionContext interceptionContext) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable1 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(IEnumerable1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable
1 migrationStatements,DbConnection connection,DbInterceptionContext interceptionContext) 在System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable1 migrationStatements, DbConnection connection) at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClass32.<ExecuteStatements>b__30() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func
1操作) 在System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action操作) 在System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable1 migrationStatements, DbTransaction existingTransaction) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable
1 migrationStatements) 在System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable1 migrationStatements) at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel, IEnumerable
1个操作,IEnumerable1 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) 在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。&lt;&gt; c__DisplayClasse.b__d() 在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.RunCore() 在System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() ClientConnectionId:8aa18649-614e-4be4-a46d-fb780984d59f 错误号码:2744,州:2,班级:16 为表'CustomerContracts'指定的多个标识列。每个表只允许一个标识列。
为什么我会收到此错误。正如你所看到的,我只使用一个键,我也没有重命名它(当我删除数据库时它是一个新表)。任何帮助表示赞赏。
这是我的迁移我在删除数据库之后删除了文件夹,但仍然将信息上下移动。
public partial class firstmigration : DbMigration
{
public override void Up()
{
DropPrimaryKey("dbo.CustomerContracts");
AddColumn("dbo.CustomerContracts", "ItemId", c => c.Int(nullable: false, identity: true));
AddPrimaryKey("dbo.CustomerContracts", "ItemId");
DropColumn("dbo.CustomerContracts", "TeamId");
}
public override void Down()
{
AddColumn("dbo.CustomerContracts", "TeamId", c => c.Int(nullable: false, identity: true));
DropPrimaryKey("dbo.CustomerContracts");
DropColumn("dbo.CustomerContracts", "ItemId");
AddPrimaryKey("dbo.CustomerContracts", "TeamId");
}
}
答案 0 :(得分:0)
试试这个:
public override void Up()
{
DropPrimaryKey("dbo.CustomerContracts");
DropColumn("dbo.CustomerContracts", "TeamId"); // Drop this first
AddColumn("dbo.CustomerContracts", "ItemId", c => c.Int(nullable: false, identity: true));
AddPrimaryKey("dbo.CustomerContracts", "ItemId");
}