How to release tracked entities?

Money Sets You Free

Hopefully the following code snippet clear enough to explain the problem. _db is an instance of DbContext.

// this scenario is only for learning purpose
Author a = _db.Authors.Find(1831);
int id = a.AuthorId;
a = null;

Author stub = new Author { AuthorId = id };
_db.Remove(stub);
_db.SaveChanges();

The code above produces

'The instance of entity type 'Author' cannot be tracked because another instance with the same key value for {'AuthorId'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.'

Question

How to free a from being tracked?

Zer0

There are a variety of ways to do this, but I find this the most simple since you're trying to detach a specific entity.

_db.Entry(a).State = EntityState.Detached

As a plus it doesn't require changing any other code, including however you fetched the entity itself.

This one line makes it very clear of the intent. It also allows the following:

  1. Fetch an entity, or list of entities
  2. Do various types of work
  3. Detach an entity

I dislike the idea of changing existing queries on a DbContext when all I want to do is detach something.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I clear tracked entities in entity framework

How to delete a file tracked by git-lfs and release the storage quota?

Previously no-tracking entities get tracked upon calling Savechages(). Is this normal?

How to update already tracked entity

How to remove tracked formatted changes

Add release from non-tracked file with Github Actions

How to stage all tracked modified files?

How to get the name of the tracked image in Swift with RealityKit?

How to ignore the contents of a previously tracked directory in git?

How to handle tracked generated files in git?

How to process tracked information in Application Insights

How are values tracked with the 'for el in sequence' syntax?

How to tell if HTML element is tracked by virtual DOM

How to list all unchanged (tracked) files?

Git: How to ignore changes to a tracked file?

How to make GIT ignore tracked file locally?

How to make Git "forget" about a file that was tracked but is now in .gitignore?

How to identify google fit activity is entered manually or tracked by sensor?

How to get Ember to detect a checkbox on change along with a glimmer tracked?

How to get correct distance between tracked entity and camera in Cesium Js?

How to find files not tracked by git and then rsync those files?

How to get previous state of a tracked entity in dbcontext after savechanges

How can I make git show a list of the files that are being tracked?

How to tell if a file is git tracked (by shell exit code)?

How to load related data for a record already being tracked by Entity Framework

How to list all distinct extensions of tracked files in a git repository?

How can we tracked the time when using Arangosh terminal?

Swift 3: How to remove the tracked polyline in Google map

How to check that all git-lfs tracked and committed files are pointers?