-
Notifications
You must be signed in to change notification settings - Fork 868
System.InvalidOperationException: The instance of entity type 'ApplicationUser' cannot be tracked because ... when trying to AddLoginAsync #914
Comments
@divega might have more info, but I believe you get this error exactly because of the fact that you are using different instances of a DbContext to manipulate the same entity. That's when I've seen exceptions regarding tracking issues in the past. In general the default identity EF store expects the DbContext to be scoped and I'm not sure what level of support EF has for modifying the same entity in different DbContex concurrently... |
@HaoK @divega Unless I'm missing something made internally by Identity, there aren't any entities being changed in different DbContex concurrently. Please provide a solution, changing the lifetime of DbContext to Scope causes EF to throw in other places we actually do have and -by design- queries that run concurrently. We don't know how to fix it and users complain about it for more than a week. 😢 |
@HaoK @gdoron actually EF Core will throw this error if two different object instances representing the same entity (i.e. having the same key value) are somehow brought into the same This could happen for example if a query materializes an From looking at the code, it seems that such situation could arise @HaoK I think we just need to try this repro steps to understand what is going on. I.e. if we are able to repro we should find out where the other instance of the user is being materialized. |
@divega Thanks! |
So the issue is between _context and _userManager's store context I guess since the user is fetched from the controller DbContext here.
And then added to the userManager context here, which throws as expected based on what @divega explained:
|
@HaoK That makes sense, but what caused Identity's DbContext to already track that user? |
Try replacing the |
Changed my code to:
Seems to fix it 🎉 I think it's still a bug, Identity should be able to handle cases where an entity is already attached, but I'm good now. BTW, I still don't understand why Identiy's dbcontext already tracked this user, maybe
is causing it? Thanks a ton for both of you @HaoK and @divega !!! (You can close the issue if you think this is the expected behavior.) |
@HaoK His question is vague and not the same as this one. His question is about having multiple DbContext types. |
In general we mostly want you to use identity APIs to deal with the user, once you start mixing DbContext operations along side what identity's default EF implementation does, well as you have already seen, there be monsters... |
FYI you still can do arbitrary queries against the users, just use _userManager.Users instead of the DbSet from your context. |
Yes, but in an other thread you told me that Identity's UserManager is Scoped, and so its DbContext is scoped as well, which means, you can't run multiple queries in parallel using UserManager. |
After chatting with @divega we expect the user to be retrieved from identity or brand new, we don't currently have the necessary APIs from EF Core to applying changes |
@divega @HaoK @rustd I saw you all (in 3 different now closed issues) asked for repro code or stacktrace for the error:
So here are exact repro steps, code and stacktrace.
Startup:
AccountController:
Exception trace:
Any insights?
If you really need, I can add you as collaborate to the private GitHub repository, it's easy to reproduce.
The text was updated successfully, but these errors were encountered: