问题描述
我正在创建此模型作为我的代码优先实体框架的一部分
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.
很明显,只指定了一个标识列。那么为什么会这样呢?
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为表指定了多个标识列。每个表只允许一个标识列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!