Repository navigation
Description
Hi. After updating EF Core to 6.0.0-preview I have started to encounter an issue that happens around 50% of the time its code is run.
The error is:
The instance of entity type 'LoginSession' cannot be tracked because another instance with the same key value for {'AccessToken'} 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.
And the code is:
private async Task UpdateSessionAndSaveChanges(LoginSession session)
{
var dbEntry = this.mainContext.Entry(session);
if (dbEntry.State == EntityState.Detached)
dbEntry.State = EntityState.Modified; // this line throws the exception
await this.mainContext.SaveChangesAsync();
dbEntry.State = EntityState.Detached;
}Explanation:
LoginSession is only ever fetched once per run and it's always fetched with .AsNoTracking(). One LoginSession is fetched on every server call for server endpoints that are decorated with a custom attribute, that fetches the session from a database (via EF Core) and verifies it.
When the client app is resumed or booted, the server session's last access time is modified and UpdateSessionAndSaveChanges is called.
UpdateSessionAndSaveChanges sets the entity's state to Modified if it's Detached (which it should be) and saves changes, so the last access time in the session entity is updated.
This has worked flawlessly until I updated EF Core to any of the 6.0.0-preview releases (I'm currently using preview4), but now around 50% of the times it's run I get the following error:
The instance of entity type 'LoginSession' cannot be tracked because another instance with the same key value for {'AccessToken'} 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.
There should only be one instance tracked as it's only ever fetched via AsNoTracking, and should only be done once per server call (where all the database contexts and controllers etc. are defined with a 'scoped' dependency injection scope).
Full stack trace:
System.InvalidOperationException:
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.ThrowIdentityConflict (Microsoft.EntityFrameworkCore, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.Add (Microsoft.EntityFrameworkCore, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.Add (Microsoft.EntityFrameworkCore, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.Add (Microsoft.EntityFrameworkCore, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking (Microsoft.EntityFrameworkCore, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState (Microsoft.EntityFrameworkCore, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState (Microsoft.EntityFrameworkCore, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry.set_State (Microsoft.EntityFrameworkCore, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at MyApp.MobileAppService.Services.AccountService+<UpdateSessionAndSaveChanges>d__26.MoveNext (MyApp.MobileAppService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=nullMyApp.MobileAppService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: /Users/myname/Projects/myproject/MyApp.MobileAppService/Services/AccountService.csMyApp.MobileAppService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 309)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at MyApp.MobileAppService.Services.AccountService+<UpdateLoginSessionAccessTime>d__8.MoveNext (MyApp.MobileAppService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=nullMyApp.MobileAppService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: /Users/myname/Projects/myproject/MyApp.MobileAppService/Services/AccountService.csMyApp.MobileAppService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 67)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at MyApp.MobileAppService.Controllers.AccountController+<RefreshSession>d__13.MoveNext (MyApp.MobileAppService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=nullMyApp.MobileAppService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: /Users/myname/Projects/myproject/MyApp.MobileAppService/Controllers/AccountController.csMyApp.MobileAppService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 107)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at lambda_method290 (Anonymously Hosted DynamicMethods Assembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+AwaitableResultExecutor+<Execute>d__0.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker+<<InvokeActionMethodAsync>g__Logged|12_1>d.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker+<<InvokeNextActionFilterAsync>g__Awaited|10_0>d.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow (Microsoft.AspNetCore.Mvc.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next (Microsoft.AspNetCore.Mvc.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker+<<InvokeInnerFilterAsync>g__Awaited|13_0>d.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker+<<InvokeNextExceptionFilterAsync>g__Awaited|25_0>d.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow (Microsoft.AspNetCore.Mvc.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next (Microsoft.AspNetCore.Mvc.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker+<<InvokeFilterPipelineAsync>g__Awaited|19_0>d.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker+<<InvokeAsync>g__Logged|17_1>d.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Routing.EndpointMiddleware+<<Invoke>g__AwaitRequestTask|6_0>d.MoveNext (Microsoft.AspNetCore.Routing, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+<Invoke>d__9.MoveNext (Microsoft.AspNetCore.Diagnostics, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
Additional information:
Server is deployed to Azure and is running Azure SQL, so the SQL Server database provider is used.
Server project uses .NET 5.
I have not seen this issue with the SQLite provider, but this is not conclusive.
Any pointers greatly appreciated!
Thank you.
Activity
Tommigun1980 commentedon Jun 9, 2021
I downgraded to EF Core 5.0.7 and the problem goes away.
[-]Reattaching and saving entity randomly stops working in EF Core 6.0.0-preview releases[/-][+]Regression in 6.0.0-preview: Reattaching and saving entity randomly stops working in EF Core/SQL Server[/+]Executor-Cheng commentedon Jun 10, 2021
I have same issue when using


AddDbContextPoolto resolveDbContext.I guess there is something wrong when a
DbContextwas returned to theDbContextPool.The screenshot shows that an abnormal
DbContext,_cachedResettableServiceshad lost something, which may cause theStateManagernot to be reset.AndriySvyryd commentedon Jun 12, 2021
@Executor-Cheng @Tommigun1980 Could either of you create a small project that shows the issue?
Executor-Cheng commentedon Jun 12, 2021
@AndriySvyryd Yes.
I guess when the
IServiceScopeis disposing, it will callDbContext.ResetStateAsync, butDbContext._contextServicesis still null.efcore/src/EFCore/DbContext.cs
Lines 824 to 825 in 7105f84
So the local field
serviceswill be null, thenStateManagerand others will not be added toresettableServices.roji commentedon Jun 13, 2021
Thanks for the good repro @Executor-Cheng and for the bug analysis - everything seems to be correct. See more minimal repro below (this isn't actually non-deterministic/random).
Minimal repro
Fix caching of resettable services for pooled uninitialized context
[-]Regression in 6.0.0-preview: Reattaching and saving entity randomly stops working in EF Core/SQL Server[/-][+]6.0 regression: pooled uninitialized context leaks state across usages[/+]Fix caching of resettable services for pooled uninitialized context (#…
Tommigun1980 commentedon Jun 13, 2021
Excellent, thank you @Executor-Cheng and @roji!
Tommigun1980 commentedon Jun 16, 2021
@roji Did the fix make it to the newly released 6.0.0-preview5.21301.9 package by any chance? Thanks.
Edit: Tested 6.0.0-preview5 and the fix is not in that package yet. I am assuming it will be in preview6 then.
roji commentedon Jun 16, 2021
Yeah, this didn't make it into preview5, but will be in preview6.
EvilVir commentedon Jun 9, 2022
I think this still is a problem. I'm on 6.0.5 and just got following error:
Unhandled exception. System.InvalidOperationException: The instance of entity type 'UserAccount.CellPhone#Contact<string>' cannot be tracked because another instance with the same key value for {'UserAccountId'} is already being tracked. When replacing owned entities, modify the properties without changing the instance or detach the previous owned entity entry first. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.The scenario where it occurs is when I have pooling DBContext in place, then somebody creates
UserAccountentry, with nullCellPhoneat this point, and then adds that phone eventually, using second method on second API call (so different DbContext from the pool most of the time). If I switch to non-pooled factory, problem disappears. I believe that added entities aren' cleared from pooled DbContext once disposed.roji commentedon Jun 9, 2022
@EvilVir can you please open a new issue with a minimal, runnable code sample that shows this happening?
EvilVir commentedon Jun 9, 2022
Will try to find time for that, hoped the description will be enough.
Anyway this whole Owned Entity concept is a nightmare, can you guys kill it already and bring back Complex Types please? Thanks.
roji commentedon Jun 9, 2022
@EvilVir we definitely clear the context's change tracker when it's returned to the pool, so the above shouldn't happen. We're going to need a repro for it.