我有这个初始迁移:
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属性是数据类型integer和主键。
我已经在schoolclassCode表中填写了数据。
现在我将数据类型从整数更改为字符串并创建了另一个迁移:
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
时,我得到一个例外,即表不允许多个标识列。
但我没有定义多个键。
有谁知道如何解决这个问题?
答案 0 :(得分:0)
尝试移动
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");
}