Error: ObjectDisposedException: Cannot access a disposed context instance in var result = await _userManager.CreateAsync(user, user.PasswordHash);

Volk Volk 571 Reputation points
2022-07-05T11:59:50.82+00:00

Hello!

I am writing a Login Form for a site on the MVC Net Core 6 Web App, but I can't find out how I can fix this error. This error occurs when I am already sending the user's data for writing to the database. Before I created the Login column in Asp Net Users to record a unique user login, registration was successful - without this error.

Updating of the database - everywhere there is a records of the types:

b.Property<string>("Login")
.Has MaxLength(256)
.HasColumnType("nvarchar(256)");

Login = table.Column<string>(maxLength: 256, nullable: true),

I tried using "using" instead of await, fix initialization app.Use(async (context, next) => (removed/changed) - doesn't help.

What do you think such an error occurs?

Thank you!

217761-k-0.png217714-k-1.png217771-k-2.png217698-k-3.png217629-k-4.png217725-k-5.png217699-k-6.png

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
698 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,214 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,151 Reputation points
    2022-07-05T13:33:17.097+00:00

    This type of error can happen if the DbContext has been disposed. Most likely the error is elsewhere in your code.

    Also, please stop post screenshots of the code. Just post the actual source code using the Code Sample button.


1 additional answer

Sort by: Most helpful
  1. Volk Volk 571 Reputation points
    2022-07-05T12:31:10.057+00:00

    I found:

    Cannot access a disposed object Object name: 'UserManager`1'
    [cannot-access-a-disposed-object-object-name-usermanager1][1]


    If another object has a reference to userManager and that object is disposed, it might be disposing userManager along with it. Another possibility is userManager requires some type of initialization which hasn't been done yet, and its properties throw a misleading disposed exception (this is common with winform controls).


    But I don't delete the login form during the execution of the method: public async Task<IActionResult> Create(Application User user)

    0 comments No comments