2相同类型EF Core 1.1的导航属性

时间:2017-09-04 13:16:30

标签: c# sql asp.net entity-framework entity-framework-core

我有2个表匹配和团队。匹配表应该包含HomeTeam和AwayTeam,但这些团队不是永久性的,每次主机创建匹配时都会创建一个新团队。所以基本上应该有一对一的

关系

HomeTeam - >比赛        和  AwayTeam - >匹配

我已经创建了这样的模型。

 public class Team
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string TeamName { get; set; }
    public Match Match { get; set; }
    public List<Player> Players { get; set; }
}
 public class Match
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public User User { get; set; }
    public Location Location { get; set; }
    public Sport Sport { get; set; }
    public DateTime Time { get; set; }
    public int Difficulty { get; set; }
    public string Type { get; set; }
    public string Comment { get; set; }
    public int HomeTeamId { get; set; }
    public int GuestTeamId { get; set; }
    public virtual Team HomeTeam { get; set; }
    public virtual Team GuestTeam { get; set; }

}

 modelBuilder.Entity<Match>()
            .HasOne(m => m.HomeTeam)
            .WithOne(m => m.Match)
            .HasForeignKey<Match>(p => p.HomeTeamId)
            .IsRequired();


        modelBuilder.Entity<Match>()
            .HasOne(m => m.GuestTeam)
            .WithOne(m => m.Match)
            .HasForeignKey<Match>(p => p.GuestTeamId)
            .IsRequired();

但是在添加迁移时,它会向我显示此错误:

Cannot create a relationship between 'Team.Match' and 'Match.GuestTeam', because there already is a relationship between 'Team.Match' and 'Match.HomeTeam'. Navigation properties can only participate in a single relationship.

0 个答案:

没有答案