title: Entity Framework Cascade Delete using Fluent API Configuration asp mvc 4
body: <p>I have two class called User and Event.</p>
  
  <p>Users can organize Events, and can have friends (User).</p>
  
  <p>User class has a many-to-many relationship with Friends, like below : </p>
  
  <pre><code>[Table("User")]
  public class User 
  {   
    public User() {
     EventOrganized = new List&lt;Event&gt;();
    }
    public virtual ICollection&lt;User&gt; Friends { get; set; }
    public virtual ICollection&lt;Event&gt; EventOrganized { get; set; }
  }
  </code></pre>
  
  <p>Event class has a one-to-many relationship with User, like below : </p>
  
  <pre><code>[Table("Event")]
  public class Event
  {
    public int OrganizerId { get; set; }
    [ForeignKey("OrganizerId")]
  
    public virtual User Organizer { get; set; }
  }
  </code></pre>
  
  <p>My Context has :</p>
  
  <pre><code>public DbSet&lt;User&gt; Users { get; set; }
  public DbSet&lt;Event&gt; Events{ get; set; }
  </code></pre>
  
  <p>And my modelBuilder configuration looks like :</p>
  
  <pre><code> protected override void OnModelCreating(DbModelBuilder modelBuilder)
   {
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity&lt;User&gt;().HasMany(u =&gt; u.Friends).WithMany().Map(
              u =&gt;
              {
                  u.MapLeftKey("UserId");
                  u.MapRightKey("FriendId");
                  u.ToTable("Friendship");
              });
    modelBuilder.Entity&lt;Event&gt;().HasRequired(e =&gt; e.Organizer).WithMany().WillCascadeOnDelete(false);
  </code></pre>
  
  <p><strong>I'd like to be able to remove a User with cascade delete</strong> (his events organized, his frienships).</p>
  
  <p><strong>Example :</strong> 
  If I delete an user, I want delete all Events organized by this user, and delete his userID in the intermediate table "frienship" between User and Friends at each row where his id appears.</p>
  
  <p>My delete method which catch Exception : </p>
  
  <pre><code>Try {
  List&lt;int&gt; friendsIds = userToRemove.Friends.Select(f =&gt; f.UserId).ToList();
      foreach (int friendID in friendsIds)
      {
        User friend = db.Users.Find(friendID);
        friend.Friends.Remove(userToRemove);
      }
   db.Users.Remove(userToRemove);
   SaveChanges();
  Catch(Exception) {}
  </code></pre>
  
  <p><strong>When User has organized an Event, Exception is :</strong>
  "The DELETE statement conflicted with the REFERENCE constraint \"FK_dbo.Event_dbo.User_OrganizerId\". 
     The conflict occurred in database \"Database\", table \"dbo.Event\", column 'OrganizerId'.</p>
  
  <p>**When User we want to delete has frienships : **
  "The DELETE statement conflicted with the REFERENCE constraint \"FK_dbo.Friendship_dbo.User_FriendId\". The conflict occurred in database \"Database\", table \"dbo.Friendship\", column 'FriendId'.</p>
  
  <p>How I am supposed to modify my Api fluent configuration ?</p>