EF: one-to-one relationship

2019-06-03 10:59发布

问题:

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).

回答1:

This works for me

namespace Ef1to1
{
    public class TestContext : DbContext
    {

        public TestContext()
            : base("Data Source=127.0.0.1;database=Junk;Integrated Security=SSPI;")
        {

        }
        public DbSet<Person> Persons { get; set; }
        public DbSet<PhoneNumber> PhoneNumbers { get; set; }
    }
    [Table("Person")]
    public class Person 
    {
        [Key, Column("PersonId")]
        public long Id { get; set; }

        public virtual PhoneNumber phoneNumber { get; set; }
    }

    [Table("PhoneNumber")]
    public class PhoneNumber
    {

        [Key, Column("PhoneNumberId"), ForeignKey("person")]
        public long Id { get; set; }

        public virtual Person person { get; set; }
    } 

   public class Program
    {

        static void Main(string[] args)
        {
            var realNumber = new PhoneNumber();
            var person = new Person() { phoneNumber = realNumber }; 
            var context = new TestContext();

            context.Persons.Add(person) ;
            context.SaveChanges();
            ;

        }
    }
}