title: The DELETE statement conflicted with the REFERENCE constraint in sql?
body: <p>I am using EF code first model. I have 3 tables <code>Teacher</code>, <code>Student</code> and <code>Address</code>. Each teacher and student can have multiple addresses, so the association between <code>Teacher_Address</code> and <code>Student_Address</code> is <em>1 to many</em>. </p>
  
  <p>For this I wrote :</p>
  
  <pre><code>public class Teacher 
  {
   public int Id;
  
   public virtual List&lt;Address&gt; Addresses { get; set; }
  }
  
  public class Student 
  {
   public int Id;
  
   public virtual List&lt;Address&gt; Addresses { get; set; }
  }
  
  public class Address
  {
   public int Id;
  
   public int? TeacherId { get; set; }
   public virtual Teacher Teacher { get; set; }
  
   public int? StudentId { get; set; }
   public virtual Student Student { get; set; }
  } 
  </code></pre>
  
  <p>Fluent Api :</p>
  
  <pre><code>        //Creating 1 to * relationships b\w Teacher and Address
          modelBuilder.Entity&lt;Address&gt;().HasOptional(p =&gt; p.Teacher)
                                        .WithMany(p =&gt; p.Addresses)
                                        .HasForeignKey(p =&gt; p.TeacherId)
                                        .WillCascadeOnDelete();
  
          //creating 1 to * replationship b\w Student and Address
          modelBuilder.Entity&lt;Address&gt;().HasOptional(p =&gt; p.Student)
                                        .WithMany(p =&gt; p.Addresses)
                                        .HasForeignKey(p =&gt; p.StudentId)
                                        .WillCascadeOnDelete();
  </code></pre>
  
  <p>I can insert data for <code>Teacher</code> and for <code>Student</code> with no issue but when I try to delete any one row out of <code>Teacher</code> and <code>Student</code> then it shows the following exception :</p>
  
  <blockquote>
    <p><em>The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.Address_dbo.Teacher_TeacherId". The conflict occurred in database "CodeFirst.TestContext", table "dbo.Address", column 'TeacherId'.</em></p>
  </blockquote>
  
  <p>Can anybody please tell me how to solve this issue and what I am doing wrong ?</p>