EF Core 2.2-同一张表的两个外键 [英] EF Core 2.2 - Two foreign keys to same table

查看:118
本文介绍了EF Core 2.2-同一张表的两个外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与此处发布的问题类似的问题:实体框架代码优先-来自同一表的两个外键,但是它已经很老了,不适用于Core,我也无法获得适合我的建议.

I have a similar problem to the one posted here: Entity Framework Code First - two Foreign Keys from same table, however it's very old and doesn't apply to Core and I can't get the suggestions to work for me.

基本上,我正在尝试创建一个夹具表,该表将具有两个到团队表的外键.固定装置由主队和客队组成.不能使用可为空的字段.

Basically, I'm trying to create a fixture table which will have two foreign keys to the team table. A fixture is made up of a home team and an away team. Having nullable fields isn't an option.

考虑一个有两个团队的固定装置.

Consider a fixture, with two teams.

public class Fixture
{
    public int Id { get; set; }
    public Team HomeTeam { get; set; }
    public int HomeTeamId { get; set; }
    public Team AwayTeam { get; set; }
    public int AwayTeamId { get; set; }

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



public class Team
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public String Name { get; set; }
    public ICollection<Fixture> HomeFixtures { get; set; } = new List<Fixture>();
    public ICollection<Fixture> AwayFixtures { get; set; } = new List<Fixture>();
}

我得到了错误...

无法确定类型为团队"的导航属性"Fixture.HomeTeam"所表示的关系.要么手动配置关系,要么使用"[NotMapped]"属性或"OnModelCreating"中的"EntityTypeBuilder.Ignore"忽略此属性.

Unable to determine the relationship represented by navigation property 'Fixture.HomeTeam' of type 'Team'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

因此,我尝试在数据库上下文中添加一些OnModelCreating代码:

So I tried to add some OnModelCreating code in the database context:

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Fixture>()
                    .HasOne(m => m.HomeTeam)
                    .WithMany(t => t.HomeFixtures)
                    .HasForeignKey(m => m.HomeTeamId)
                    .OnDelete(DeleteBehavior.Restrict);


        modelBuilder.Entity<Fixture>()
                    .HasOne(m => m.AwayTeam)
                    .WithMany(t => t.AwayFixtures)
                    .HasForeignKey(m => m.AwayTeamId)
                    .OnDelete(DeleteBehavior.Restrict);
    }

然后我得到了错误:

Introducing FOREIGN KEY constraint 'FK_Fixtures_Teams_HomeTeamId' on table 'Fixtures' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

有人可以帮助您进行此设置吗?

Can anyone help with getting this setup please?

谢谢.

推荐答案

public class Fixture
{
    public int Id { get; set; }
    public int HomeTeamId { get; set; }
    public int AwayTeamId { get; set; }

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

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

通过这种方式导航将起作用.还应@Ivan的建议,删除重复的getter和setter.

This way navigation will work. Also as suggested by @Ivan remove duplicate getters and setters.

这篇关于EF Core 2.2-同一张表的两个外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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