EL MOJO
EL MOJO

Reputation: 793

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
{
    [Key]
    public Guid Id { get; set; }

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

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

    [ForeignKey( "PolicyHolderId" )]
    public Person PolicyHolder { get; set; }
    [Required]
    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.

Upvotes: 4

Views: 2977

Answers (1)

EL MOJO
EL MOJO

Reputation: 793

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

Upvotes: 5

Related Questions