0

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?

Elisabeth
  • 20,496
  • 52
  • 200
  • 321
  • 1
    That's a known issue - you will need to recreate the table. See Get's answer here: http://stackoverflow.com/questions/20153233/cant-remove-identity-attribute-from-pk – Steve Greene Mar 30 '16 at 21:46

1 Answers1

0

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");
}
kevin_fitz
  • 837
  • 6
  • 19
  • I tried all sorts of reordering the migration methods and your suggestion returns this: Could not create IDENTITY attribute on nullable column 'schoolclass', But where is the nullable string column 'schoolclass' and primary key column? The DropPrimaryKey is executed before the AlterColumn method !!! Thats not logical! – Elisabeth Mar 30 '16 at 19:17
  • I correct: "...a primary key column?" – Elisabeth Mar 30 '16 at 20:01
  • That won't work once you already have an identity field unless you recreate the table. – Steve Greene Mar 30 '16 at 21:48
  • But when I recreate the table with the migration sql methods do I only have to do CreateTable("Schoolclasscode") and will my fluent configurations also be used? – Elisabeth Mar 31 '16 at 09:42
  • @steve is right. You're going to have to manually script out backup, drop, and recreate scripts using the Sql(); command. Sql("//Select * INTO BackupTable From Original Table"); Sql("//Drop Existing Table"); Sql("//Create New Table"); Sql("//Restore Data From Back Up"); Sql("//Delete Backup"); – kevin_fitz Mar 31 '16 at 20:20