title: Both One-To-One and One-To-Many relationships in Entity Framework 5 Code First
body: <p>i tried the whole day to get this working. I learned a lot about EF's Fluent API (e.g. <a href="http://weblogs.asp.net/manavi/archive/2010/12/19/entity-association-mapping-with-code-first-one-to-one-shared-primary-key-associations.aspx" rel="noreferrer">this</a> is an excellent article), however i had no success.</p>
  
  <p>I have three Entities:</p>
  
  <pre><code>public class Address
  {
     [Key]
     public virtual int AddressId { get; set; }
     public virtual string AddressString { get; set; }
  }
  public class User
  {
     [Key]
     public virtual int UserId { get; set; }
     public virtual ICollection&lt;Address&gt; Addresses { get; set; }
  }
  public class House
  {
     [Key]
     public virtual int HouseId { get; set; }
     public virtual Address Address { get; set; }
  }
  </code></pre>
  
  <p>and tried all combinations of <code>HasMany, HasOptional, WithOptional, WithOptionalDependent</code> and <code>WithOptionalPrincipial</code> i could think of for both <code>User</code> and <code>House</code> in the </p>
  
  <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder)
  </code></pre>
  
  <p>I just cannot get it to work. I think it should be clear, what i want. A User may have more than one address (in the first place i want to force at least one, but now i would be happy if a user may have the addresses optional...) while a House has exactly one Address - and this is required. It would be nice if the address of a house would be cascading deleted.</p>