我正在创建此模型作为我的代码第一个实体框架的一部分
public class NewUserRegistration
{
[Key]
public int NewUserRegistrationId { get; set; }
}
在Package Manger Console中使用Update-Database -Verbose -Force
命令在更新Applying automatic migration: 201211252223088_AutomaticMigration.
ALTER TABLE [dbo]。[NewUserRegistration] ADD [NewUserRegistrationId] [int] NOT NULL IDENTITY System.Data.SqlClient.SqlException (0x80131904):为表指定的多个标识列 'NewUserRegistration'。每个表只允许一个标识列。 在System.Data.SqlClient.SqlConnection.OnError(SqlException异常, 布尔值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,布尔& dataReady)at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(字符串 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次操作,布尔值 降级,布尔自动)at 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(IEnumerable的1 pendingMigrations, String targetMigrationId, String lastMigrationId)
1 pendingMigrations,String targetMigrationId,String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable
在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 Multiple 为表'NewUserRegistration'指定的标识列。只有一个 允许每个表的标识列。
显然只指定了一个标识列。那么为什么会这样呢?
当我这样做时,我也不例外。
public class NewUserRegistration
{
[Key]
public int Id { get; set; }
}
有关为何会出现这种情况的任何想法?
修改 的
我应该说我正在更改密钥的名称。评论说你不能这样做。我该如何删除并重新创建?
最好从SQL中删除数据库,然后再次运行Update-Database
命令吗?
答案 0 :(得分:72)
尝试重命名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");
}
希望对您的案件有所帮助。
答案 1 :(得分:5)
我只是使用DropPrimaryKey
命令替换相关的DropColumn
,AddColumn
,AddPrimaryKey
和RenameColumn
命令也没有任何问题,例如< / p>
public partial class RenameKey : DbMigration
{
public override void Up()
{
RenameColumn("dbo.GameSummary", "OldId", "Id");
}
}
答案 2 :(得分:2)
我第一次迁移后也遇到了类似的问题。我意识到,删除第一次迁移创建的数据库,然后删除了在我的mvc应用程序中创建的迁移文件夹后,问题再次没有出现。
答案 3 :(得分:1)
您可以使用以下内容直接从类中更改列的名称:
[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; }
}
}
答案 4 :(得分:1)
第一个Add-Migration -Name
然后
public override void Up()
{
RenameColumn("dbo.Department", "OldId", "NewId");
}
答案 5 :(得分:0)
最佳实施方案是:
/*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");