title: EF: how to delete many-to-many relationship without loading more than is needed
body: <p>Given the entities: <code>Topics</code> and <code>Subscriptions</code> with many-to-many relationship and the corresponding tables: <code>Topics</code>, <code>Subscriptions</code>, <code>TopicsSubscriptions</code>, I want to delete only one row from <code>TopicsSubscriptions</code> using EF, but without loading any extra data (except of maybe only 1 topic and 1 subscription). </p>
  
  <p>In the end I want EF to generate similar SQL to this:</p>
  
  <pre><code>exec sp_executesql N'delete [dbo].[TopicSubscriptions]
  where (([TopicId] = @0) and ([SubscriptionId] = @1))',N'@0 int,@1 int',@0=1,@1=2
  </code></pre>
  
  <p>I have <code>LazyLoading</code> set to <code>false</code>.</p>
  
  <p>I thought I could take the answer from here: <a href="https://stackoverflow.com/questions/757067/how-to-delete-many-to-many-relationship-in-entity-framework-without-loading-all">How to delete many-to-many relationship in Entity Framework without loading all of the data</a>. </p>
  
  <blockquote>
  <pre><code>var db = new TopicDBEntities(); var topic = db.Topics.FirstOrDefault(x =&gt; x.TopicId == 1);
  
  // Get the subscription you want to delete var subscription =
  db.Subscriptions.FirstOrDefault(x =&gt; x.SubscriptionId == 2);
  
  // !!! This line does not work for me (Attach to ICollection&lt;&gt; ?!?) !!!
  topic.Subscriptions.Attach(subscription); // Attach it (theObjectContext now 'thinks' it belongs to the topic)
  
  topic.Subscriptions.Remove(subscription); // Remove it
  db.SaveChanges(); // Flush changes
  </code></pre>
  </blockquote>
  
  <p>But then I realized there is no <code>Attach</code> method belonging to <code>ICollection&lt;&gt;</code>... unless I am missing something.</p>
  
  <p>Idea of attaching only one subscription to a topic sounds good, but I cannot understand how to achieve it.</p>
  
  <p>I am using <code>DbContext</code> instead of <code>ObjectContext</code> and Entity Framework 6 but I guess it should not matter.</p>
  
  <p><strong>EDIT:</strong> Would be good to find a solution without stored procedures, or direct sql, if possible, since I have to support many database back-ends in my app.</p>
  
  <p>In case I was not clear enough, I don't need to delete the entities, I just need to remove the association between them.</p>