title: Entity Framework on delete cascade
body: <p>I have problem with deleting related rows in Entity Framework 4.1. I have tables with relations</p>
  
  <p>Book  1&lt;--->* BookFormats</p>
  
  <p>I have set the on delete cascade:</p>
  
  <pre><code>ALTER TABLE [dbo].[BookFormats]  WITH CHECK ADD  CONSTRAINT [FK_BookFormats_Book] 
  FOREIGN KEY([BookID]) REFERENCES [dbo].[Book] ([BookID]) on delete cascade
  </code></pre>
  
  <p>The EDMX property</p>
  
  <p><img src="https://i.stack.imgur.com/np3mv.png" alt="enter image description here"></p>
  
  <p>Then, I want to remove the all <code>BokFormats</code> items related to my <code>Book</code> object:</p>
  
  <pre><code> var originalBook = m.db.Book.First(x =&gt; x.BookID == bookId);
   originalBook.BookFormats.Clear();
   m.db.SaveChanges();
  </code></pre>
  
  <p>But, I get the error:</p>
  
  <blockquote>
    <p>The operation failed: The relationship could not be changed because
    one or more of the foreign-key properties is non-nullable. When a
    change is made to a relationship, the related foreign-key property is
    set to a null value. If the foreign-key does not support null values,
    a new relationship must be defined, the foreign-key property must be
    assigned another non-null value, or the unrelated object must be
    deleted.</p>
  </blockquote>
  
  <p>I ran out of ideas on how to delete these objects. Any ideas?</p>