简体   繁体   English

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

[英]Updating Entity Framework Database by migration

I'm working on my ASP.NET MVC application with Entity Framework. 我正在使用Entity Framework开发ASP.NET MVC应用程序。 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 错误号:2744,状态:2,类别:16
Multiple identity columns specified for table 'PersonModel'. 为表“ 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; }
}

Looks like you've changed your primary key column from PersonId to IdPerson . 好像您已将主键列从PersonId更改为IdPerson The error is being thrown because the latter is added before the former is removed. 之所以引发该错误,是因为后者是在删除前者之前添加的。 Rearrange your Up() method like so: 重新排列您的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));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM