为表异常指定的多个标识列 [英] Multiple identity columns specified for table exception
本文介绍了为表异常指定的多个标识列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个初始迁移:
namespace DataAccess.Migrations
{
using System.Data.Entity.Migrations;
public partial class init : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.SchoolclassCodes",
c => new
{
Schoolclass = c.Int(nullable: false, identity: true),
Type = c.String(),
})
.PrimaryKey(t => t.Schoolclass);
}
public override void Down()
{
DropTable("dbo.SchoolclassCodes");
}
}
}
schoolclass属性是数据类型整数和主键。
The schoolclass property is of datatype integer and the primary key.
我已经在schoolclassCode表中填写了数据。
I have already data filled in the schoolclassCode table.
现在我更改了数据类型从整数到字符串,并创建了另一个迁移:
Now I changed the datatype from integer to string and created another migration:
namespace DataAccess.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class changedatattypeandaddedkeyId : DbMigration
{
public override void Up()
{
DropPrimaryKey("dbo.SchoolclassCodes");
AddColumn("dbo.SchoolclassCodes", "Id", c => c.Int(nullable: false, identity: true));
AlterColumn("dbo.SchoolclassCodes", "Schoolclass", c => c.String());
AddPrimaryKey("dbo.SchoolclassCodes", "Id");
}
public override void Down()
{
DropPrimaryKey("dbo.SchoolclassCodes");
AlterColumn("dbo.SchoolclassCodes", "Schoolclass", c => c.Int(nullable: false, identity: true));
DropColumn("dbo.SchoolclassCodes", "Id");
AddPrimaryKey("dbo.SchoolclassCodes", "Schoolclass");
}
}
}
当我运行 update-database
我得到一个例外,表中不允许有多个标识列。
When I run the update-database
I get the exception that multiple identity columns are not allowed for a table.
但是我没有多个键
有谁知道如何解决这个问题?
Does anyone know how to solve this?
推荐答案
尝试移动
AlterColumn("dbo.SchoolclassCodes", "Schoolclass", c => c.String());
以上
AddColumn("dbo.SchoolclassCodes", "Id", c => c.Int(nullable: false, identity: true));
所以
public override void Up()
{
DropPrimaryKey("dbo.SchoolclassCodes");
AlterColumn("dbo.SchoolclassCodes", "Schoolclass", c => c.String());
AddColumn("dbo.SchoolclassCodes", "Id", c => c.Int(nullable: false, identity: true));
AddPrimaryKey("dbo.SchoolclassCodes", "Id");
}
这篇关于为表异常指定的多个标识列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文