entity-framework-coreef-core-5.0

Entity Framework Core 5 - Error with recursive structure


I'm trying to setup an entity framework class which has 4 fields that link back to others of the same type or are null. My class looks like this:

public class Patch : EntityBase
{
    [Key]
    public int PatchId { get; set; }

    [ForeignKey("NorthPatchId")]
    public virtual Patch NorthPatch { get; set; }

    [ForeignKey("SouthPatchId")]
    public virtual Patch SouthPatch { get; set; }

    [ForeignKey("EastPatchId")]
    public virtual Patch EastPatch { get; set; }

    [ForeignKey("WestPatchId")]
    public virtual Patch WestPatch { get; set; }
}

This works fine if I only have NorthPatch and SouthPatch but as soon as I add the third one, EastPatch, I get the following error while trying to do the migration:

System.InvalidOperationException: Unable to determine the relationship represented by navigation 'Patch.NorthPatch' of type 'Patch'.


Solution

  • That's a pretty cool bug! I was able to duplicate, AND as a bonus found the bug reported and still open for EF Core.

    Open Bug: https://github.com/dotnet/efcore/issues/21968

    Similar Issue: Entity Framework Core One-One Self Referencing Relationship fails

    Workaround: Remove the [ForeignKey] attributes, and use the following to your OnModelConfiguring for your context instead.

    builder.Entity<Patch>()
        .HasOne(x => x.NorthPatch)
        .WithOne()
        .HasForeignKey(typeof(Patch), "NorthPatchId");
    
    builder.Entity<Patch>()
        .HasOne(x => x.SouthPatch)
        .WithOne()
        .HasForeignKey(typeof(Patch), "SouthPatchId");
    
    builder.Entity<Patch>()
        .HasOne(x => x.EastPatch)
        .WithOne()
        .HasForeignKey(typeof(Patch), "EastPatchId");
    
    builder.Entity<Patch>()
        .HasOne(x => x.WestPatch)
        .WithOne()
        .HasForeignKey(typeof(Patch), "WestPatchId");