efcore: The instance of entity type cannot be tracked because another instance with the same key value for { 'ID'} is already being tracked.
// https://stackoverflow.com/questions/62253837/the-instance-of-entity-type-cannot-be-tracked-because-another-instance-with-the // https://github.com/dotnet/efcore/issues/12459 // https://docs.microsoft.com/en-us/ef/core/change-tracking/identity-resolution
.net 6.0 EF core 6.0.8 Project attached.
As commented in the code the problem I am having is discovering if “another instance” even exists and if it does where the mysterious “another instance” is. If “another instance” does in fact exist, why is it not being assigned to series in the line below?:
// Next line we set series to the instance of series being tracked by the db.
// If there is "another instance", series should be set
// to that "another instance" since we are directly assigning it:
series = db.Series.First(x => x.Symbol == symbol);
I do not see any logical path forward to save series to disk. In the space of two lines of code I am reading an entity from disk and trying to write it back and am unable to do so. How do I modify series and save it to disk?
using Microsoft.EntityFrameworkCore;
public class Program
{
private const string connectionString = "Data Source=.;Initial Catalog=EFDupeTest;Integrated Security=True;MultipleActiveResultSets=True;";
private Db db;
public Program(Db db) => this.db = db;
public static async Task Main(string[] args)
{
DbContextOptionsBuilder builder = new DbContextOptionsBuilder();
builder.UseSqlServer(connectionString);
Db db = new Db(builder.Options);
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
await new Program(db).DownloadSeriesRelease("xyz");
}
public async Task DownloadSeriesRelease(string symbol)
{
Series? series = db.Series.FirstOrDefault(x => x.Symbol == symbol);
if (series == null)
await DownloadSeries(symbol);
// Next line we set series to the instance of series being tracked by the db.
// If there is "another instance", series should be set
// to that "another instance" since we are directly assigning it:
series = db.Series.First(x => x.Symbol == symbol);
//
// series has been saved to disk and now has an ID of 1.
// In real code we modify a property of series here and save to disk.
// How do we save series to disk?
//
// Error here
// System.InvalidOperationException
// The instance of entity type 'Series' cannot be tracked because another instance with the same key
// value for { 'ID'} 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.
db.Entry(series).State = series.ID == 0 ? EntityState.Added : EntityState.Modified;
await db.SaveChangesAsync();
}
public async Task DownloadSeries(string symbol)
{
Series series = new Series { Symbol = symbol };
await SaveSeries(series);
}
public async Task SaveSeries(Series series)
{
Series? dupe = await db.Series.FirstOrDefaultAsync(x => x.ID != series.ID && x.Symbol == series.Symbol);
db.Entry(series).State = series.ID == 0 ? EntityState.Added : EntityState.Modified;
await db.SaveChangesAsync();
}
}
public class Series
{
public int ID { get; set; }
public string Symbol { get; set; }
}
public class Db : DbContext
{
public DbSet<Series> Series { get; set; }
public Db(DbContextOptions options) : base(options)
{
InitalizeContext();
}
protected virtual void InitalizeContext()
{
// https://blog.oneunicorn.com/2012/03/12/secrets-of-detectchanges-part-3-switching-off-automatic-detectchanges/
ChangeTracker.AutoDetectChangesEnabled = false;
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
Database.SetCommandTimeout(360);
}
}
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 44 (22 by maintainers)
@smitpatel I don’t need your remarks.
@sam-wheat The code first creates and saves a
Seriesinstance. This instance is now being tracked by the context. The code then queries again using no-tracking. This creates a new instance of the same entity. The code then attempts to track this duplicate instance.Because the query has tracking disabled.
It’s not clear to me why the code is using a no-tracking query for this. The code doesn’t throw if tracking is used appropriately. You might want to read up on the basics of change tracking.
@roji
As stated previously, I provided that snippet to demonstrate what my code does NOT do. I documented that clearly in the code.
In my original post please identify the specific line where I instruct EF to start tracking a different instance with the same ID.
Please do not close this ticket until this question is answered. Thank you.
@ajcvickers
Because there is no other instance of
series.This line in
DownloadSeriesReleaseis the first and only place where series is assigned a non null value. And that value per the documentation I cited is not tracked. There are no other instances ofseries.series = db.Series.First(x => x.Symbol == symbol); // not tracked
@ajcvickers addressed things above, but as I have an in-progress comment, I’ll post it anyway…
This is where your understanding of EF’s API contracts/obligations is off. QueryTrackingBehavior.NoTracking only means that queries don’t track - note the “Query” part in the enum name. The Entry() is a completely separate API which has nothing to do with querying, and therefore isn’t affected by that setting. You can use Entry() to start tracking an arbitrary instance, regardless of whether it came from an EF query, instantiated directly, or whatever.
As written above, that is exactly what QueryTrackingBehavior.NoTracking does. But you’re using an entirely separate API (Entry) to very explicitly tell EF to start tracking the untracked instance returned from your non-tracking query.
So there are no contradictory obligations here, just two different APIs - querying and Entry().
Here again, it seems like you have certain assumptions/expectations which simply don’t correspond to how EF works. To quote from the introductory docs on change tracking:
Note that as we’ve written above, SaveChanges does not cause anything to stop getting tracked. Among other things, there are valid scenarios where one may want to continue tracking after SaveChanges, so clearing at that point would make those scenarios impossible.
I think it’s fine to question to EF’s design decisions here, even if there’s little chance we’d change this (e.g. because of the breaking change). But I highly recommend first understanding the EF way of doing things; in this case, that would be via a plain, regular tracking query. Once you have a firm grasp on how EF works, you’d at the very least be in a better place to understand why some of these decisions are made.
@sam-wheat
This is not a “normal pattern”. In fact, it is explicitly called out in the docs as an anti-pattern–see https://docs.microsoft.com/en-us/ef/core/change-tracking/explicit-tracking#introduction, which states “Attaching entities to the same DbContext instance that they were queried from should not normally be needed. Do not routinely perform a no-tracking query and then attach the returned entities to the same context. This will be slower than using a tracking query, and may also result in issues such as missing shadow property values, making it harder to get right.”
The correct thing to do, as I indicated in the very first comment on this thread, is to do a tracking query here. You will then get back the tracked entity instance.
You need to learn how to use the APIs in the manner they were intended to be used. This is why we have documentation.
This is not a good an analogy. The two things are not similar in any relevant way.
The natural life time is the unit-of-work, which should be associated with the context lifetime. Again, this is called out in the documentation.
Your suggestions reflect your expectations of how an ORM should work. Maybe they would result in a good ORM; maybe not. It’s not the way EF works and never will be.
@roji
I think the problem here is that EF has contradictory obligations related to tracking. The byproduct of EF trying to simultaneously respect these mutually excusive requirements inflicts more harm than the good either obligation provides.
QueryTrackingBehavior.NoTrackingtells EF to not track. CallingEntry(...)API tells EF to track. To satisfy both EF creates a duplicate entity - the effect of which, as indicated by this ticket, creates a minefield.Now we are clear about how the dupe is being created and who is responsible. Why is it that by following a very normal pattern of retrieving data I can cause EF to commit this awful deed? What pattern can I follow that will prevent this and that will not enlist me in the business of managing tracking status on behalf of EF? Fair questions!
Should I expect something else? I expect EF to expose APIs that allow me to to use it in a consistent manner.
I think of tracking in a manner similar to locking a table in sql server. Sometimes it must be done but in all cases the time spent doing it should be minimized.
Entry(..)provides a mechanism to track but it has no natural end to its lifetime that EF can manage. Clearing tracking flag whenSaveChangesis called will allow for a pattern that will:Limit tracking lifetime to two lines of code that are naturally contiguous.
Preclude the need for EF to create a dupe when returning an entity.
Allow EF to return objects that are in a consistent state.
Allow the developer to offload the task of managing tracking state to EF.
Allow
QueryTrackingBehavior.NoTrackingto live up to it’s promise to not track any of the entities that are returned from a LINQ query.@sam-wheat we’re done that above as well, but here goes once more. Looking at the code in the OP:
series == null). It creates a new instance of Series and saves it to the database. After that completes and DownloadSeries returns, the context is tracking that instance (even if the variable is out of scope).series = db.Series.First(x => x.Symbol == symbol)). Since the context is configured with AutoDetectChangesEnabled=false, this is a non-tracking query which bypasses the change tracker. That means it returns a new, unrelated instance in series. This in itself isn’t a problem, but note that the earlier instance (created and saved inside DownloadSeries) is still tracked by the context.db.Entry(series)(2nd before last), passing it the untracked instance from step 2 above. This explicitly asks EF to start tracking the instance, but the earlier instance is still tracked, and has the same ID. Therefore, EF raises an exception.I’m again guessing that you expect EF to stop tracking the 1st instance when your code returns from DownloadSeries (since it “goes out of scope”, or since SaveChangesAsync has been called) - this is not the case. I highly suggest stepping through the code with your debugger, line-by-line, and examining the ChangeTracker property on the context (see @ajcvickers’s screenshots above). This tells you unambiguously what is currently tracked by the context.
Once all of the above is clear and you have the right mental model of change tracking, we can suggest an alternative code pattern to do what you’re trying to achieve. But it’s important to first understand why your current code doesn’t work.
@sam-wheat we’ve done that several times already, but looking at your sample in https://github.com/dotnet/efcore/issues/28822#issuecomment-1222576059
In other words, db.Entry is an API which tells EF “start tracking this”.
@roji
Kindly refer me to the line in the example I’ve provided where I do this.
What good, than, is
QueryTrackingBehavior.NoTracking? This behavior is self-defeating.I don’t make that assumption at all. I am guided by a specific section of documentation which I have cited many times and will cite again:
// The change tracker will not track any of the entities that are returned from a LINQ query.
I read this to mean that I should be able run the query shown below with complete confidence that the entity (entities) I receive is/are not tracked. This seems very clear to me.
Do you mean to say that the entity is referenced by by the dbcontext, which would prevent it from being garbage collected? That seems reasonable. As for tracking behavior, here are the events that I expect to occur:
DownloadSeries- new up an entity.SaveSeries- Set the state to Modified, thus telling EF to track the entity.SaveSeries- Call SaveChanges(). At this point the entity should revert to being untracked. EF’s work is done.DownloadSeriesReleasewhere I assign new entity toseries. At this point I should be able to have full faith and confidence that the entity returned as a result of my LINQ query is not tracked. This is whatQueryTrackingBehavior.NoTracking;assures me will happen but appears to not happen.How many instances of series are there?
@ajcvickers
lol Arthur did you read what you just wrote?
Ok nothing is tracked…
Oh wait…
What? I’m not using a tracking query??
Ok lets just take the average… lolol
@ajcvickers
There is only one instance. Furthermore, this directly contradicts the documentation for
QueryTrackingBehavior.NoTracking. The documentation fairly describes the behavior I expect from the setting :// The change tracker will not track any of the entities that are returned from a LINQ query. If the /// entity instances are modified, this will not be detected by the change tracker and /// will not persist those changes to the database.
Here is the behavior I expect to happen AFTER ‘SaveChanges’ is called in ‘SaveSeries’:
The method returns to
DownloadSeriesand then toDownloadSeriesRelease. The local instance of series that was created inDownloadSeriesis disposed. The next line of code that executes is this one:series, which was null prior to the call toDownloadSeriesshould be set to an untracked instance ofSeries. This is the place whereQueryTrackingBehavior.NoTrackingmust be respected. There are no other instances of series that exist at this point. Even if the context returns the instance of series that was created inDownloadSeries,seriesis simply pointing to that instance. There is only one instance and it is not tracked.This line in
DownloadSeriesReleaseshould not fail. There is only one instance ofseriesand it is not tracked:Does that line throw an error? Why is it relevant?
The line that throws the error this one in
DownloadSeriesRelease, where I retrieve an untracked entity from the context and attempt to set it’s state to added:I don’t track the first instance. Line 66 is the method declaration for
DownloadSeries. Is there another place in the code where I trackseries? Please cite the line.What is the specific line number in my code where I do that?
@ajcvickers
// Next line we set series to the instance of series being tracked by the db.
This comment in my code is actually wrong. The instance we are getting is untracked per previously cited documentation related to
QueryTrackingBehavior.NoTracking.So, bottom line, I have a single instance of
serieswhich is not tracked and EF is crashing telling me there is another tracked instance.@ajcvickers
I can’t set the state to Added. EF crashes. That is the problem.
Did you try running my code? Please run the code in the project or in my original post.
@ajcvickers please erase that modification and use the code I originally posted. I added that subsequent code to show how my code is different. I also commented “in call to DownloadSeriesRelease this goes out of scope”:
@ajcvickers
It does not. It throws the error I cited in my code.
Hopefully we agree that it should start tracking and we have made some progress.
@ajcvickers
In my code the second instance of
seriesthat is created inDownloadSeriesgoes out of scope. InDownloadSeriesReleaseI assign the result of a linq query to my single instance ofseries. Per this documentation forQueryTrackingBehavior.NoTrackingthat instance is not tracked:// The change tracker will not track any of the entities that are returned from a LINQ query. If the /// entity instances are modified, this will not be detected by the change tracker and /// will not persist those changes to the database.
Please explain how this code should be written assuming
QueryTrackingBehavior.NoTrackingis used as default behavior. You have referred me to the documentation several times - I do not understand how to apply it to my code. Also, the documentation I have cited appears to support my use case. How am I intrepetting it incorrectly?Thanks.
@ajcvickers To simplify things a bit I modified
Mainas shown below. The lines between thetest 1comments follow the general sequence of my code however thesecondSeriesvariable does not go out of scope as it does in the call toDownloadSeriesRelease.In my code the instance of
seriescreated inDownloadSeriesgoes out of scope. However the error message is telling me “another instance” exists. Is the actual error that the instance I am trying to track is already tracked? If so, why is it tracked? The following, from QueryTrackingBehavior.NoTracking intellisense is true, correct?;// The change tracker will not track any of the entities that are returned from a LINQ query. If the /// entity instances are modified, this will not be detected by the change tracker and /// <see cref="DbContext.SaveChanges()" /> will not persist those changes to the database.
Hi @ajcvickers ,
Which line of code are you referring to? I believe the first step is to create a dbcontext with tracking disabled.
I’ve been using the settings shown for several years now and I have not run into this issue. Note the link in the
InitalizeContextmethod in the code.