I am trying to set up a bi-directional one-to-one relationship. However, I am failing to get a bi-directional setting for two entities.
For example, lets say one person has one phone number, and one phone number can only be associated to one person. I would have:
public class Person : Entity
{
public long PersonId { get; set; }
public virtual PhoneNumber PhoneNumber {get; set; }
}
public class PhoneNumber : Entity
{
public long PhoneNumberId { get; set; }
}
However, with this re-arrangement, I cannot get a bi-directional setting. i.e. I cannot have phoneNumber.Person.
What I have tried:
public class PhoneNumber : Entity
{
public long PhoneNumberId { get; set; }
[InverseProperty("PersonId")]
public virtual Person Person {get; set; }
}
This does not seem to work when I execute the following code:
var realNumber = new PhoneNumber();
var person = new Person() {PhoneNumber = realNumber};
context.SaveChanges();
Here, the PhoneNumber entity has a column Person (set to NULL), and the person has a column for PhoneNumber (has value).