Multiple identity columns specified for table exce

2019-09-10 00:15发布

This question already has an answer here:

I have this initial migration:

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");
        }
    }
}

The schoolclass property is of datatype integer and the primary key.

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");
        }
    }
}

When I run the update-database I get the exception that multiple identity columns are not allowed for a table.

But I have nowhere multiple keys defined.

Does anyone know how to solve this?

1条回答
ら.Afraid
2楼-- · 2019-09-10 00:44

Try moving

AlterColumn("dbo.SchoolclassCodes", "Schoolclass", c => c.String());

above

AddColumn("dbo.SchoolclassCodes", "Id", c => c.Int(nullable: false, identity: true));

So

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");
}
查看更多
登录 后发表回答