Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UserManager.AddToRoleAsync with enable options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) throw exception #3429

Open
magals opened this issue Jan 8, 2025 · 0 comments

Comments

@magals
Copy link

magals commented Jan 8, 2025

I am creating methods for registering users with a default role. I found that if the options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) option is enabled, an exception occurs when adding a role to a user.

image

"System.InvalidOperationException: 'The instance of entity type 'User' 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.'"

As soon as I turn off QueryTrackingBehavior.NoTracking, the method works without problems.
The service where NoTracking works is not small, and I did not want to turn it off option.

Is it possible to fix this?

If not, it would be nice to add text to the exception that would help to understand the problem. I spent a day to understand what the reason was, since I added authentication using lessons/reports by other authors, and each time it led to a stupor when everything works out in the author's lesson, and I have an exception

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant