EF Core One to One or Zero Relationship

Here

.HasForeignKey<Person>(a => a.PersonID)

you are telling EF that Person.PersonID will be a FK (foreign key) to Address, i.e. Person is dependent and is referencing the principal Address.

It should be other way around:

.HasForeignKey<Address>(a => a.PersonID)

This way, Person (the principal) will have 0..1 Address, and Address (the dependent) will have 1 Person (because the PersonId is both PK and FK).

This is called Shared Primary Key association and is the standard (and default) way of modelling one to zero or one relationship in EF Core.

For more info, see Relationships.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)