1

I'm working on my ASP.NET MVC application with Entity Framework. After enabling migration and adding a migration like this:

public override void Up()
{
        DropForeignKey("dbo.PersonModel", "TeamModel_TeamId", "dbo.TeamModel");
        DropIndex("dbo.PersonModel", new[] { "TeamModel_TeamId" });
        RenameColumn(table: "dbo.PersonModel", name: "TeamModel_TeamId", newName: "TeamModelRefId");
        DropPrimaryKey("dbo.PersonModel");
        AddColumn("dbo.PersonModel", "IdPerson", c => c.Int(nullable: false, identity: true));
        AlterColumn("dbo.PersonModel", "TeamModelRefId", c => c.Int(nullable: false));
        AddPrimaryKey("dbo.PersonModel", "IdPerson");
        CreateIndex("dbo.PersonModel", "TeamModelRefId");
        AddForeignKey("dbo.PersonModel", "TeamModelRefId", "dbo.TeamModel", "TeamId", cascadeDelete: true);
        DropColumn("dbo.PersonModel", "PersonId");
}

public override void Down()
{
        AddColumn("dbo.PersonModel", "PersonId", c => c.Int(nullable: false, identity: true));
        DropForeignKey("dbo.PersonModel", "TeamModelRefId", "dbo.TeamModel");
        DropIndex("dbo.PersonModel", new[] { "TeamModelRefId" });
        DropPrimaryKey("dbo.PersonModel");
        AlterColumn("dbo.PersonModel", "TeamModelRefId", c => c.Int());
        DropColumn("dbo.PersonModel", "IdPerson");
        AddPrimaryKey("dbo.PersonModel", "PersonId");
        RenameColumn(table: "dbo.PersonModel", name: "TeamModelRefId", newName: "TeamModel_TeamId");
        CreateIndex("dbo.PersonModel", "TeamModel_TeamId");
        AddForeignKey("dbo.PersonModel", "TeamModel_TeamId", "dbo.TeamModel", "TeamId");
}

I get an error like this:

Error Number:2744,State:2,Class:16
Multiple identity columns specified for table 'PersonModel'. Only one identity column per table is allowed.

This is my model:

public class PersonModel
{
    [Key]
    public int IdPerson { get; set; }
    public string NickName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }

    public int TeamModelRefId { get; set; }

    [ForeignKey("TeamModelRefId")]    
    public virtual TeamModel TeamModel { get; set; }
}

public class TeamModel
{
    public TeamModel()
    {
        TeamMembers = new List<PersonModel>();
        this.Tournaments = new HashSet<TournamentModel>();
    }

    [Key]
    public int TeamId { get; set; }

    public string TeamName { get; set; }

    public virtual ICollection<PersonModel> TeamMembers { get; set; }

    public ICollection<TournamentModel> Tournaments { get; set; }

    public virtual MatchUpEntryModel MatchupEntry { get; set; }

    public virtual MatchUpModel Matchup { get; set; }
}

1 Answer 1

2

Looks like you've changed your primary key column from PersonId to IdPerson. The error is being thrown because the latter is added before the former is removed. Rearrange your Up() method like so:

public override void Up()
    {
        DropForeignKey("dbo.PersonModel", "TeamModel_TeamId", "dbo.TeamModel");
        DropIndex("dbo.PersonModel", new[] { "TeamModel_TeamId" });
        RenameColumn(table: "dbo.PersonModel", name: "TeamModel_TeamId", newName: "TeamModelRefId");
        DropPrimaryKey("dbo.PersonModel");
        AddPrimaryKey("dbo.PersonModel", "IdPerson");
        CreateIndex("dbo.PersonModel", "TeamModelRefId");
        AddForeignKey("dbo.PersonModel", "TeamModelRefId", "dbo.TeamModel", "TeamId", cascadeDelete: true);
        DropColumn("dbo.PersonModel", "PersonId");
        AddColumn("dbo.PersonModel", "IdPerson", c => c.Int(nullable: false, identity: true));
        AlterColumn("dbo.PersonModel", "TeamModelRefId", c => c.Int(nullable: false));
    }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.