title: Entity Framework Navigation Collection Property Delete
body: <p>I have two entities as the following:</p>
  
  <pre><code>public class FirstEntity
  {
      public int Id { get; set; }
  
      public ICollection&lt;SecondEntity&gt; SecondEntityCollection { get; set; }
  }
  
  public class SecondEntity
  {
      [Key]
      [Column(Order = 0)]
      public int FirstEntitySomeId { get; set; }
  
      [Key]
      [Column(Order = 1)]
      public int SecondEntityId { get; set; }
  }
  </code></pre>
  
  <p>My <code>ModelBuilder</code> is set as following</p>
  
  <pre><code>modelBuilder.Entity&lt;FirstEntity&gt;()
              .HasMany(x =&gt; x.SecondEntity)
              .WithRequired()
              .HasForeignKey(x =&gt; x.FirstEntitySomeId);
  </code></pre>
  
  <p>When deleting a <code>FirstEntity</code> object I do the following:</p>
  
  <pre><code>var firstEntity=this.context.FirstEntities.Where(x=&gt;x.Id==someId);
  firstEntity.SecondEntityCollection.Clear();
  this.context.FirstEntities.Remove(firstEntity);
  this.context.SaveChanges();
  </code></pre>
  
  <p>However I still get the following exception:</p>
  
  <blockquote>
    <p>The DELETE statement conflicted with the REFERENCE constraint "....". The conflict occurred in database "Database", table "dbo.SecondEntity", column 'FirstEntitySomeId'.</p>
  </blockquote>
  
  <p>How can I properly clear the collection before deleting the <code>firstEntity</code> object? </p>
  
  <p>It's worth mentioning that I am trying to <em>avoid</em> cascade deleting. I do want to delete the orphan entries in the database. However if the best solution/practice is to use cascade delete I will use it.</p>
  
  <p>Is this possible?</p>