Ask a questionAsk a question
 

QuestionRelationships (One to One) in EF 4.0

  • Friday, November 06, 2009 8:16 AMSoulfly Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hello,

    i hava a question about relationships in Beta 2 with Code Only Model. How to specify a one to one
    relationship? E.g. i have two tables / classes "Person" and "PersonAddress".
    public class Person
    {
      public PersonId { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public PersonAddress Address { get; set; }
    }
    
    public class PersonAddress
    {
      public PersonAddressId { get; set; }
      public string Street { get; set ; }
      // ...
    }
    
    The "PersonAddress" is nullable in "Person".
    So, how to specify the mapping for the class "Person"?
    public PersonMap()
    {
       MapSingleType(p => new
          {
            PersonID = p.PersonId,
            FirstName = p.FirstName,
            LastName = p.LastName,
            PersonAddressID = // p.Address?? or p.Address.PersonAddressId??
          }).ToTable("dbo.Persons");
    
        HasKey(p => p.PersonId);
               
        Relationship(//???
    }
    
    Can someone give an advise?

All Replies