title: Entity Framework Code First: How can I model a Customer / Address relationship?
body: <p>Here is my simplified model:</p>
  
  <pre><code>public class Customer
  {
      public int ID { get; set; }
      public int MailingAddressID { get; set; }
      [ForeignKey("MailingAddressID")]
      public Address MailingAddress { get; set; }
  
      public virtual ICollection&lt;Address&gt; Addresses { get; set; }
  }
  
  public class Address
  {
      public int ID { get; set; }
      public int CustomerID { get; set; }
      [ForeignKey("CustomerID")]
      public Customer Customer { get; set; }
  }
  </code></pre>
  
  <p>When I try and create the database I get the following error:</p>
  
  <blockquote>
    <p>Introducing FOREIGN KEY constraint 'Customer_MailingAddress' on table 'Customers' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
    Could not create constraint. See previous errors. </p>
  </blockquote>
  
  <p>I don't understand what the problem is. I understand that a Customer with an Address cannot be deleted and also that an Address which is a Customer's Mailing Address can also not be deleted. </p>
  
  <p>This fits with my design because if a Customer has one or more addresses, then one must be a mailing address and that address can not be deleted.</p>
  
  <p>So what am I missing here? Thanks!</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Forgot to mention that I tried adding the following line in the OnModelBuilding method:</p>
  
  <pre><code>modelBuilder.Entity&lt;Customer&gt;().Property(x =&gt; x.MailingAddressID).IsOptional();
  </code></pre>
  
  <p>This allows the database to be built, however when adding a customer I get the following error:</p>
  
  <blockquote>
    <p>The INSERT statement conflicted with the FOREIGN KEY constraint "Customer_MailingAddress". The conflict occurred in database "DomainModel.SeasideHeightsEntities", table "dbo.Addresses", column 'ID'.
    The statement has been terminated. </p>
  </blockquote>
  
  <p>Still at a loss as to how to model this properly.</p>