实体框架code首先 - 从同一个表中两个外键 [英] Entity Framework Code First - two Foreign Keys from same table

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

问题描述

我刚开始先使用EF code,所以我本主题共有初学者。

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

我想创造队和比赛之间的关系:
1场= 2队(主场,客人)和结果。我想它很容易建立这样的模式,所以我就开始编码:

I wanted to create relations between Teams and Matches: 1 match = 2 teams (home, guest) and result. I thought It's easy to create such 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外键相同的表?
TIA。

How can I create such model, with 2 foreign keys to same table ? TIA.

推荐答案

试试这个:

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);
    }
}

主键默认情况下,约定映射。团队必须有两个集合比赛。你不能有两个FKS引用单个集合。比赛时没有级联删除,因为它没有在这些工作自引用映射多到很多。

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.

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

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