通过迁移更新实体框架数据库

时间:2017-07-24 18:23:11

标签: c# asp.net-mvc entity-framework

我正在使用Entity Framework处理我的ASP.NET MVC应用程序。启用迁移并添加如下迁移后:

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

我收到这样的错误:

  

错误号码:2744,州:2,等级:16
  为表' PersonModel'指定了多个标识列。每个表只允许一个标识列。

这是我的模特:

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 个答案:

答案 0 :(得分:2)

您似乎已将主键列从PersonId更改为IdPerson。抛出错误是因为后者是在删除前者之前添加的。像这样重新排列你的Up()方法:

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