migration error: Using the same entity twice

migration error: Using the same entity twice

实体:

public class Match
{
    [Key]
    public int MatchId { get; set; }
    public int HomeTeamId { get; set; }
    public int AwayTeamId { get; set; }
    public string HomeTeamScore { get; set; }
    public string AwayTeamScore { get; set; }
    public string Stadium { get; set; }
    public string MatchDate { get; set; }
    public string MatchTime { get; set; }
    public string InsertDate { get; set; }
    public int Status { get; set; }

    [ForeignKey("HomeTeamId")]
    public virtual Team HomeTeam { get; set; }

    [ForeignKey("AwayTeamId")]
    public virtual Team AwayTeam { get; set; }
}

public class Team
{
    public int TeamId { get; set; }
    public string TeamName { get; set; }
    public virtual ICollection<Match> Matches { get; set; }
}

internal sealed class Configuration : DbMigrationsConfiguration<BalikciContext>
{

    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = false;
    }

    protected override void Seed(BalikciContext context)
    {

    }
}

错误:在 table 'Matches' 上引入 FOREIGN KEY 约束 'FK_dbo.Matches_dbo.Teams_AwayTeamId' 可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。 无法创建约束。查看以前的错误。

有什么问题?

谢谢。

Entity Framework 将无法确定 Matches 集合指向的位置(Home 还是 Away?)。你需要这样的属性(也可以使用流利的 api):

public class Team
{
    public int TeamId { get; set; }
    public string TeamName { get; set; }

    [InverseProperty("HomeTeamId")] 
    public virtual ICollection<Match> HomeMatches { get; set; }

    [InverseProperty("AwayTeamId")] 
    public virtual ICollection<Match> AwayMatches { get; set; }
}

https://msdn.microsoft.com/en-us/data/jj591583.aspx#Relationships