Victor
Victor

Reputation: 941

Why can't the child/dependent side be determined for this one-to-one relationship?

I am getting the usual error of:

The child/dependent side could not be determined for the one-to-one relationship between 'Person.UserAccount' and 'UserAccount.Person'. To identify the child/dependent side of the relationship, configure the foreign key property. If these navigations should not be part of the same relationship configure them without specifying the inverse. See http://go.microsoft.com/fwlink/?LinkId=724062 for more details.

In my case, UserAccount is a slave of Person, they have a relationship of one-to-zero/one and the foreign key is the field Id. I have set it up as follows:

modelBuilder.Entity<Person>()
            .HasOne(e => e.UserAccount)
            .WithOne(e => e.Person)
            .IsRequired()
            .HasForeignKey<UserAccount>(e => e.Id);

I have tried a whole other lot of combinations, read through the docs and other questions, but no matter what it's just not able to determine the child/dependent side.

UserAccount class:

[Table("UserAccount")]
public class UserAccount : EntityBase
{
    public int Id { get; set; }

    public Person Person { get; set; }

}

Person class:

    [Table("PERSON")]
public partial class Person : EntityBase
{
    public Guid Id { get; set; }

    [Key]
    public int PersonId { get; set; }

    public UserAccount UserAccount { get; set; }

}

What is going on? what am I missing?

Upvotes: 0

Views: 6536

Answers (1)

charbel
charbel

Reputation: 491

In your PERSON table you have 2 properties that can possibly be a foreign key property which resulted in EF core being confused. Can you try adding [ForeignKey("PersonId")] in UserAccount Class:

[Table("UserAccount")]
public class UserAccount : EntityBase
{
    public int Id { get; set; }

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

}

EDIT: Try also renaming your properties accordingly.

Upvotes: 2

Related Questions