title: .NET, Entity Framework: objects removal - which is faster/better?
body: <p>Sometimes I have to face removing many rows at one time. It costs large amount of time (for example removing 3000 elements + ~21000 related elements costs me approx. 4 minutes).</p>
  
  <p>The way I do it:</p>
  
  <pre><code>// removes settlement elements
  var settlementElements = context.SettlementElements.Where(x =&gt; x.SettlementElementSettlementId == id);                     context.SettlementElements.RemoveRange(settlementElements);
  </code></pre>
  
  <p>I wonder if getting only IDs of those elements, create empty objects with those IDs, attach them and remove would be faster than remove range used above? I mean sth like this:</p>
  
  <pre><code>var settlementElementsIds = context.SettlementElements.Where(x =&gt; x.SettlementElementSettlementId == id).Select(x =&gt; x.SettlementElementId);
  foreach (var seID in settlementElementsIds)
  {
      var obj = new SettlementElements() { SettlementElementId = seID };
      context.SettlementElements.Attach(obj);
      context.SettlementElements.Remove(obj);
  }
  </code></pre>
  
  <p>Do you have some experience in optimizing elements removal?</p>