dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.52k stars 10.04k forks source link

Something strange with UserManager's users tracking #58892

Open Lavshyak opened 1 week ago

Lavshyak commented 1 week ago

Is there an existing issue for this?

Describe the bug

servicesScope0:
var user = new User
userManager0.CreateAsync(user

IdentityUser userToRemember;
servicesScope1:
userToRemember = userManager1.FindByIdAsync(1)

//the presence of this block does not affect the occurrence of an exception, I added it for example
serviceScope2:
tmpUser = userManager2.FindByIdAsync(1)
userManager2.UpdateSecurityStampAsync(tmpUser) // ok

serviceScope3:
userManager3.UpdateSecurityStampAsync(userToRemember) //throws InvalidOperationException exception

Expected Behavior

no exception

Steps To Reproduce

Project with code to reproduce: https://github.com/Lavshyak/AspNetIdentityUserManagerBug

Exceptions (if any)

InvalidOperationException with message: "The instance of entity type 'Account' 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..."

.NET Version

8.0.403 9.0.100

Anything else?

ASP.NET 8.0.10 ASP.NET 9.0.0 Rider 2024.2.7

dotnet --info when .net 8:

.NET SDK: Version: 8.0.403 Commit: c64aa40a71 Workload version: 8.0.400-manifests.e99c892e MSBuild version: 17.11.9+a69bbaaf5

Runtime environment: OS Name: Windows OS Version: 10.0.22631 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\8.0.403\

Installed .NET Workloads: Configured to use loose manifests when installing new manifests. There are no installed workloads to display.

Host: Version: 8.0.10 Architecture: x64 Commit: 81cabf2857

.NET SDKs installed: 8.0.403 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 8.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 8.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 8.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables: Not set

global.json file: Not found

dotnet --info when .net 9:

.NET SDK: Version: 9.0.100 Commit: 59db016f11 Workload version: 9.0.100-manifests.c6f19616 MSBuild version: 17.12.7+5b8665660

Runtime environment: OS Name: Windows OS Version: 10.0.22631 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\9.0.100\

Installed .NET Workloads: Configured to use loose manifests when installing new manifests. There are no installed workloads to display.

Host: Version: 9.0.0 Architecture: x64 Commit: 9d5a6a9aa4

.NET SDKs installed: 9.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables: Not set

global.json file: Not found

Lavshyak commented 1 week ago

when i do this, there is no exception: serviceScope3: mainDbContext.Attach(userToRemember)

so i don't think the problem is in DbContext

Lavshyak commented 1 week ago

and it's no matter i remember account from servicesScope0 or servicesScope1. so i think the problem is either in the UserManager from scope0 or from scope3