EF Core 2.0 Unable to Determine Relationship



I just recently upgraded my solution/project(s) from .NET Core 1.1 to 2.0, including EntityFramework Core. I am now getting an error I never got from EF Core 1.1.

"Unable to determine the relationship represented by navigation property 'InsurancePolicy.Person' of type 'Person'."

Class/Entity definition is below.

public class InsurancePolicy
    public Guid Id { get; set; }

    [ForeignKey( "InsuranceCompanyId" )]
    public InsuranceCompany InsuranceCompany { get; set; }
    public int InsuranceCompanyId { get; set; }

    [ForeignKey( "PersonId" )]
    public Person Person { get; set; }
    public Guid PersonId { get; set; }

    [ForeignKey( "PolicyHolderId" )]
    public Person PolicyHolder { get; set; }
    public Guid PolicyHolderId { get; set; }

    public string GroupId { get; set; }
    public string MemberId { get; set; }

If I remove the Person/PersonId property, I just get the same error but on a different entity/property. Something else is going on or the way this is done in EF 2.0 changed. This worked fine with EF 1.1 and I was able to run my migration and deploy the database. After the upgrade to 2.0, I cannot make any calls to my DbContext without getting the above error.

8/17/2017 3:16:00 PM

Popular Answer

This is a known issue within EF 2.0.

The work around for now is to explicitly define the relationship using the Fluent API.

For example (from the link provided):

modelBuilder.Entity<Relation>(e =>
       e.HasOne(r => r.AccountManager).WithMany(u => u.AccountManagerRelations).HasForeignKey(r => r.AccountManagerId);
       e.HasOne(r => r.SalesManager).WithMany(u => u.SalesManagerRelations).HasForeignKey(r => r.SalesManagerId);
8/17/2017 5:50:07 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow