title: EF 4.1 Many to Many Delete Parent should delete related children
body: <p>EF 4.1 Database First approach.</p>
  
  <p>Say I have this table schema </p>
  
  <blockquote>
    <p>Users 1---M UserRoles M---1 Roles</p>
  </blockquote>
  
  <p>Cascade delete is setup in the Foreign Keys</p>
  
  <p>The UserRoles table has additional columns like CreatedDate so I create a model for UserRoles and map accordingly. </p>
  
  <p>I end up with the following Models:</p>
  
  <pre><code>User
  ----
  int Id
  string Name
  List&lt;UserRoles&gt; UserRoles
  
  UserRoles
  ---------
  int UserId
  int RoleId
  DateTime CreatedDate
  User User
  Role Role
  
  Role
  ----
  int Id
  string Name
  List&lt;UserRoles&gt; UserRoles
  </code></pre>
  
  <p>If I have my configuration correct, should I be able to delete a user and will the user roles rows be deleted WITHOUT having to clear the UserRoles collection manually?</p>
  
  <p>So can I just do this:</p>
  
  <pre><code>DbContext.Entry(user).State = EntityState.Deleted;
  DbContext.SaveChanges();
  </code></pre>
  
  <p>Or do I HAVE to do this:</p>
  
  <pre><code>user.UserRoles.Clear();
  DbContext.Entry(user).State = EntityState.Deleted;
  DbContext.SaveChanges();
  </code></pre>
  
  <p>My testing shows I HAVE to clear the child collection, but I find conflicting information that if I have cascade delete setup correctly it should work by only deleting the User. </p>
  
  <p>When I DON'T clear the UserRoles I receive this error:</p>
  
  <blockquote>
    <p>The relationship could not be changed because one or more of the
    foreign-key properties is non-nullable</p>
  </blockquote>
  
  <p>Thanks for you help in clarifying this!</p>