实体框架代码优先 - 来自同一个表的两个外键

时间:2011-04-05 21:57:03

标签: c# entity-framework orm code-first entity-framework-4.1

我刚刚开始使用EF代码,所以我在本主题中完全是初学者。

我想在团队和比赛之间建立关系: 1场比赛= 2队(主场,客场)和结果。我认为创建这样的模型很容易,所以我开始编码:

public class Team
{
    [Key]
    public int TeamId { get; set;} 
    public string Name { get; set; }

    public virtual ICollection<Match> Matches { get; set; }
}


public class Match
{
    [Key]
    public int MatchId { get; set; }

    [ForeignKey("HomeTeam"), Column(Order = 0)]
    public int HomeTeamId { get; set; }
    [ForeignKey("GuestTeam"), Column(Order = 1)]
    public int GuestTeamId { get; set; }

    public float HomePoints { get; set; }
    public float GuestPoints { get; set; }
    public DateTime Date { get; set; }

    public virtual Team HomeTeam { get; set; }
    public virtual Team GuestTeam { get; set; }
}

我得到一个例外:

  

参照关系将导致不允许循环引用。 [约束名称= Match_GuestTeam]

如何创建这样的模型,同一个表有2个外键? TIA。

6 个答案:

答案 0 :(得分:277)

试试这个:

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

    public virtual ICollection<Match> HomeMatches { get; set; }
    public virtual ICollection<Match> AwayMatches { get; set; }
}

public class Match
{
    public int MatchId { get; set; }

    public int HomeTeamId { get; set; }
    public int GuestTeamId { get; set; }

    public float HomePoints { get; set; }
    public float GuestPoints { get; set; }
    public DateTime Date { get; set; }

    public virtual Team HomeTeam { get; set; }
    public virtual Team GuestTeam { get; set; }
}


public class Context : DbContext
{
    ...

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Match>()
                    .HasRequired(m => m.HomeTeam)
                    .WithMany(t => t.HomeMatches)
                    .HasForeignKey(m => m.HomeTeamId)
                    .WillCascadeOnDelete(false);

        modelBuilder.Entity<Match>()
                    .HasRequired(m => m.GuestTeam)
                    .WithMany(t => t.AwayMatches)
                    .HasForeignKey(m => m.GuestTeamId)
                    .WillCascadeOnDelete(false);
    }
}

主键按默认约定映射。团队必须有两个比赛集合。您不能拥有两个FK引用的单个集合。匹配是在没有级联删除的情况下映射的,因为它在这些自引用多对多中不起作用。

答案 1 :(得分:50)

也可以在导航属性中指定ForeignKey()属性:

[ForeignKey("HomeTeamID")]
public virtual Team HomeTeam { get; set; }
[ForeignKey("GuestTeamID")]
public virtual Team GuestTeam { get; set; }

这样您就不需要向OnModelCreate方法

添加任何代码

答案 2 :(得分:36)

我知道它已有几年的历史了,您可以通过上述解决方案解决您的问题。但是,我只想建议对仍然需要的人使用InverseProperty。至少你不需要在OnModelCreating中改变任何东西。

以下代码未经过测试。

public class Team
{
    [Key]
    public int TeamId { get; set;} 
    public string Name { get; set; }

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

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


public class Match
{
    [Key]
    public int MatchId { get; set; }

    public float HomePoints { get; set; }
    public float GuestPoints { get; set; }
    public DateTime Date { get; set; }

    public virtual Team HomeTeam { get; set; }
    public virtual Team GuestTeam { get; set; }
}

您可以在MSDN上阅读有关InverseProperty的更多信息:https://msdn.microsoft.com/en-us/data/jj591583?f=255&MSPPError=-2147217396#Relationships

答案 3 :(得分:13)

你也可以试试这个:

public class Match
{
    [Key]
    public int MatchId { get; set; }

    [ForeignKey("HomeTeam"), Column(Order = 0)]
    public int? HomeTeamId { get; set; }
    [ForeignKey("GuestTeam"), Column(Order = 1)]
    public int? GuestTeamId { get; set; }

    public float HomePoints { get; set; }
    public float GuestPoints { get; set; }
    public DateTime Date { get; set; }

    public virtual Team HomeTeam { get; set; }
    public virtual Team GuestTeam { get; set; }
}

当您使FK列允许NULLS时,您正在打破循环。或者我们只是在欺骗EF模式生成器。

就我而言,这个简单的修改解决了这个问题。

答案 4 :(得分:12)

这是因为默认情况下启用Cascade Deletes。问题是,当您在实体上调用删除时,它也会删除每个f键引用的实体。您不应该使“必需”值可以为空来解决此问题。更好的选择是删除EF Code First的级联删除约定:

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); 

在映射/配置时,明确指示何时为每个子级执行级联删除可能更安全。实体。

答案 5 :(得分:1)

EF Core中的

InverseProperty使解决方案变得简单而干净。

InverseProperty

因此所需的解决方案是:

public class Team
{
    [Key]
    public int TeamId { get; set;} 
    public string Name { get; set; }

    [InverseProperty(nameof(Match.HomeTeam))]
    public ICollection<Match> HomeMatches{ get; set; }

    [InverseProperty(nameof(Match.GuestTeam))]
    public ICollection<Match> AwayMatches{ get; set; }
}


public class Match
{
    [Key]
    public int MatchId { get; set; }

    [ForeignKey(nameof(HomeTeam)), Column(Order = 0)]
    public int HomeTeamId { get; set; }
    [ForeignKey(nameof(GuestTeam)), Column(Order = 1)]
    public int GuestTeamId { get; set; }

    public float HomePoints { get; set; }
    public float GuestPoints { get; set; }
    public DateTime Date { get; set; }

    public Team HomeTeam { get; set; }
    public Team GuestTeam { get; set; }
}