title: Deleting EF Core entity via Id using attach/remove results in InvalidOperationException
body: <p>I have an EF Core database context set up that has a particular set of objects. I want to be able to delete these objects from my database via their Id. However I don't want to query the database and extract these objects as they're quite big and I'm trying to avoid the performance overhead.</p>
  <p><a href="https://stackoverflow.com/questions/2471433/how-to-delete-an-object-by-id-with-entity-framework">Looking at this link</a> it seems like a simple enough procedure. Find your Ids, create temporary objects of the same type and then attach them to the context, then remove them.</p>
  <p>However when I do this, I get the following exception.</p>
  <blockquote>
  <p>The instance of entity type 'Type' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked.</p>
  </blockquote>
  <p>I assume this means that because there is already an instance of the object being tracked, I can't then load a second instance of the object into memory using the same id.</p>
  <p>How then can I delete these objects using their Id? Loading the original database objects into memory is completely out of the question.</p>
  <pre><code>var policySetIdsToDelete = _configDbContext.PolicySets
                      .Where(ps =&gt; ps.SerialNo == serialNumber.ToUpper())
                      .OrderByDescending(ps =&gt; ps.Created).Skip(5).Select(ps =&gt; ps.Id.ToString()).ToList();
                  
  foreach (var id in policySetIdsToDelete)
  {
      var policySet = new PolicySet
                      {
                          Id = new Guid(id)
                      };
      _configDbContext.PolicySets.Attach(policySet);
      _configDbContext.PolicySets.Remove(policySet);
  }
  
  _configDbContext.SaveChanges();
  </code></pre>