EF Core self referencing not working if there are 2 foreign keys to self (Code first)

故事扮演 提交于 2019-12-10 09:40:32

问题


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

and so is this:

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

but not this:

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

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).

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.


回答1:


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


来源:https://stackoverflow.com/questions/46827795/ef-core-self-referencing-not-working-if-there-are-2-foreign-keys-to-self-code-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!