实体框架代码优先 - 来自同一个表的两个外键 [英] Entity Framework Code First - two Foreign Keys from same table

查看:35
本文介绍了实体框架代码优先 - 来自同一个表的两个外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 EF 代码,所以我完全是这个主题的初学者.

I've just started using EF code first, so I'm a total beginner in this topic.

我想在团队和比赛之间建立关系:

I wanted to create relations between Teams and Matches:

1 场比赛 = 2 支球队(主队、客队)和结果.

1 match = 2 teams (home, guest) and result.

我认为创建这样的模型很容易,所以我开始编码:

I thought it's easy to create such a model, so I started coding:

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 ]

The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = Match_GuestTeam ]

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

How can I create such a model, with 2 foreign keys to the same table?

推荐答案

试试这个:

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 引用的单个集合.匹配是在没有级联删除的情况下映射的,因为它在这些多对多的自引用中不起作用.

Primary keys are mapped by default convention. Team must have two collection of matches. You can't have single collection referenced by two FKs. Match is mapped without cascading delete because it doesn't work in these self referencing many-to-many.

这篇关于实体框架代码优先 - 来自同一个表的两个外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆