title: How to disable cascade delete for link tables in EF code-first?
body: <p>I want to disable cascade deletes for a link table with entity framework code-first.  For example, if many users have many roles, and I try to delete a role, I want that delete to be blocked <em>unless</em> there are no users currently associated with that role.  I already remove the cascade delete convention in my <code>OnModelCreating</code>:</p>
  
  <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder) {
      ...
      modelBuilder.Conventions.Remove&lt;OneToManyCascadeDeleteConvention&gt;();
  </code></pre>
  
  <p>And then I set up the user-role link table:</p>
  
  <pre><code>modelBuilder.Entity&lt;User&gt;()
      .HasMany(usr =&gt; usr.Roles)
      .WithMany(role =&gt; role.Users)
      .Map(m =&gt; {
          m.ToTable("UsersRoles");
          m.MapLeftKey("UserId");
          m.MapRightKey("RoleId");
      });
  </code></pre>
  
  <p>Yet when EF creates the database, it creates a delete cascade for the foreign key relationships, eg.</p>
  
  <pre><code>ALTER TABLE [dbo].[UsersRoles]  WITH CHECK ADD  CONSTRAINT [FK_dbo.UsersRoles_dbo.User_UserId] FOREIGN KEY([UserId])
  REFERENCES [dbo].[User] ([UserId])
  ON DELETE CASCADE
  GO
  
  ALTER TABLE [dbo].[UsersRoles]  WITH CHECK ADD  CONSTRAINT [FK_dbo.UsersRoles_dbo.Role_RoleId] FOREIGN KEY([RoleId])
  REFERENCES [dbo].[Role] ([RoleId])
  ON DELETE CASCADE
  GO
  </code></pre>
  
  <p>How can I stop EF generating this delete cascade?</p>