title: How to delete related fields using Entity Framework
body: <p>I have an object like this : </p>
  
  <pre><code>public class MyObject
  {
      [Key]
      public int Id { get; set; }
  
      public string ExternalNumber { get; set; }
  
  
      public virtual InternalProperties InProperties { get; set; }
  
      public virtual ICollection&lt;AdditionalPrice&gt; PriceHistory{ get; set; }
  }
  </code></pre>
  
  <p>And InternalProperties class : </p>
  
  <pre><code>public class InternalProperties
  {
      [Key]
      public int Id { get; set; }
      public string  Field1{get; set;}
  }
  </code></pre>
  
  <p>And PriceHistory : </p>
  
  <pre><code>public class AdditionalPrice
  {
      [Key]
      public int Id { get; set; }
      public double? Price { get; set; }
  }
  </code></pre>
  
  <p>And my context look like this : </p>
  
  <pre><code>public class myContext : DbContext
  {
          static myContext ()
          {
              Database.SetInitializer&lt;myContext &gt;(null);
          }
  
          public myContext ()
              : base("myCtx")
          {
          }
  
          public DbSet&lt;MyObject&gt; Objects{ get; set; }
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
             modelBuilder.Entity&lt;myObject&gt;().HasOptional(m =&gt; m.InProperties );
             modelBuilder.Entity&lt;myObject&gt;().HasMany(m =&gt; m.PriceHistory);
          }
  
  }
  </code></pre>
  
  <p>Finally I'm trying to delete myObjects like this :</p>
  
  <pre><code>            using (var _sdb = new MyContext())
              {
                  _sdb.Configuration.AutoDetectChangesEnabled = false;
                  var myList = _sdb.Objects.Where(m =&gt; m.Id &gt; 5).OrderBy(c =&gt; c.Id).ToList();
                  foreach(var obj in myList)
                  {
                   _sdb.Objects.Remove(obj)
                  }
  
              }
  _sdb.SaveChanges();
  </code></pre>
  
  <p>I'm getting this exception at SaveChanges(): </p>
  
  <blockquote>
    <p>{"An error occurred while saving entities that do not expose foreign
    key properties for their relationships. The EntityEntries property
    will return null because a single entity cannot be identified as the
    source of the exception. Handling of exceptions while saving can be
    made easier by exposing foreign key properties in your entity types.
    See the InnerException for details."}</p>
  </blockquote>
  
  <p>And Inner Exception says : </p>
  
  <blockquote>
    <p>{"The DELETE statement conflicted with the REFERENCE constraint
    \"FK_dbo.AdditionalPrice_dbo.MyObject_MyObject_Id\". The conflict
    occurred in database \"myobject\", table \"dbo.AdditionalPrice\",
    column 'myObject_Id'.\r\nThe statement has been terminated."}</p>
  </blockquote>
  
  <p>The exceptions are pretty explanatory but I don't know how to delete these objects safely. I appreciate any help. Thanks in advance.</p>