Efcore: Multiple self-referencing foreign keys: Unable to determine the relationship represented by navigation property. -5.0.0-preview.7

Created on 7 Aug 2020  路  3Comments  路  Source: dotnet/efcore

Creating a migration for the following entity:

public class Person
{
    public long Id { get; set; }

    public string Name { get; set; }

    public long? MotherId { get; set; }

    [ForeignKey(nameof(MotherId))]
    public Person Mother { get; set; }

    public long? FatherId { get; set; }

    [ForeignKey(nameof(FatherId))]
    public Person Father { get; set; }

    public long? StephMotherId { get; set; }

    [ForeignKey(nameof(StephMotherId))]
    public Person StepMother { get; set; }
}

Steps to reproduce

create a migration for the person entity.

This isn't working on 3.1.6 either.

Further technical details

EF Core version: 5.0.0-preview.7
Database provider: sqlserver
Target framework: .NET Core 3.1)
Operating system: windows 10 vs 2019. -16.6.5

area-model-building customer-reported type-bug

All 3 comments

You need to configure the relationship manually using Fluent API or InversePropertyAttribute. See our documentation for more details.

Note to triage: When ForeignKeyAttribute is specified on both navigations we split the relationship into two, therefore there are no ambiguous candidates in this scenario, so we don't need to throw.

@AndriySvyryd It is working for two self-referencing foreign keys but adding a third breaks it.

Was this page helpful?
0 / 5 - 0 ratings