如果有两个自身的外键,则EF Core自引用不起作用(代码优先) [英] EF Core self referencing not working if there are 2 foreign keys to self (Code first)

查看:275
本文介绍了如果有两个自身的外键,则EF Core自引用不起作用(代码优先)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我(首先在代码中)为自身定义了一个导航属性,则它可以工作(创建外键),但是如果我定义2,则它不起作用。

如何创建2个自我的外键?基于文档

If I defined (in code first) one navigation property to self it works (foreign key is created), but if I define 2 it doesn't work.
How can I create 2 foreign keys to self? Based on documentation by conventions they should be created.

例如,此方法有效(创建了外键):

For example this works (foreign key is created):

public class DbPart 
{
    [Key]
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [ForeignKey("Source")]
    public int? SourceId { get; set; }
    public DbPart Source { get; set; }

    [InverseProperty("Source")]
    public List<DbPart> SourceParts { get; set; }
}

也是这样:

public class DbPart 
{
    [Key]
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [ForeignKey("SourceFake")]
    public int? SourceFakeId { get; set; }
    public DbPart SourceFake { get; set; }

    [InverseProperty("SourceFake")]
    public List<DbPart> SourceFakeParts { get; set; }
}

但不是这样:

public class DbPart 
{
    [Key]
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [ForeignKey("Source")]
    public int? SourceId { get; set; }
    public DbPart Source { get; set; }


    [ForeignKey("SourceFake")]
    public int? SourceFakeId { get; set; }
    public DbPart SourceFake { get; set; }

    [InverseProperty("SourceFake")]
    public List<DbPart> SourceFakeParts { get; set; }

    [InverseProperty("Source")]
    public List<DbPart> SourceParts { get; set; }
}

我还有另一个例子,我在数据库中写树结构,但是而不是只写ParentId,我也写RootId。同样,在将2个属性引用到self(ParentId,RootId)时,不会创建外键。

I also have another example, where I write tree structure in database, but instead writing just ParentId, I also write RootId. Again, foreign key is not created when referencing 2 properties to self (ParentId, RootId).

已编辑:

由于错误,它无法正常工作。简便的解决方案是从ID属性中删除[Key]或在Steve Greene答案中使用流利的解决方案。还请此处

Edited:
It is not working because of this bug. Easy solution is to remove [Key] from Id property or use fluent solution in Steve Greene answer. Check also here.

推荐答案

我更喜欢这些内容的流利代码:

I prefer the fluent code for this stuff:

modelBuilder.Entity<DbPart>()
    .HasOne(p => p.Source)
    .WithMany(p => p.SourceParts)
    .HasForeignKey(p => p.SourceId);

modelBuilder.Entity<DbPart>()
    .HasOne(p => p.SourceFake)
    .WithMany(p => p.SourceFakeParts)
    .HasForeignKey(p => p.SourceFakeId);

但是如果您想要注释,请尝试以下操作:

But if you want annotations try this:

public class DbPart 
{
    public int Id { get; set; }   // Key & Indentity by convention

    public int? SourceId { get; set; }  // FK by convention
    [InverseProperty("SourceParts")]
    public DbPart Source { get; set; }

    public int? SourceFakeId { get; set; } // FK by convention
    [InverseProperty("SourceFakeParts")]
    public DbPart SourceFake { get; set; }

    [InverseProperty("SourceFake")]
    public List<DbPart> SourceFakeParts { get; set; }

    [InverseProperty("Source")]
    public List<DbPart> SourceParts { get; set; }
}

这篇关于如果有两个自身的外键,则EF Core自引用不起作用(代码优先)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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