Multiple identity columns specified for table exception [duplicate]

二次信任 提交于 2019-12-12 03:55:48

问题


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:


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


来源:https://stackoverflow.com/questions/36317283/multiple-identity-columns-specified-for-table-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!