question

VolkVolk-1263 avatar image
0 Votes"
VolkVolk-1263 asked VolkVolk-1263 commented

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

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


dotnet-aspnet-core-mvcdotnet-entity-framework-coredotnet-aspnet-core-auth
k-0.png (43.4 KiB)
k-1.png (264.2 KiB)
k-2.png (138.7 KiB)
k-3.png (26.4 KiB)
k-4.png (30.5 KiB)
k-5.png (16.2 KiB)
k-6.png (55.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered VolkVolk-1263 commented

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The problem is resolved! Thank you!
I shouldn't have used using (_db), so _db was deleted after checking. :) :) :)

 public bool IsLoginAvailble(string login)
          {
              //using (_db)
              //{
                  try
                  {
                      var tag = _db.ApplicationUsers.Single(m => m.Login == login);
                      return true;
                  }
                  catch (Exception)
                  {
                      return false;
                  }
              //}
          }
0 Votes 0 ·

The problem is resolved! Thank you!
I shouldn't have used using (_db), so _db was deleted after checking. :) :) :)

 public bool IsLoginAvailble(string login)
          {
              //using (_db)
              //{
                  try
                  {
                      var tag = _db.ApplicationUsers.Single(m => m.Login == login);
                      return true;
                  }
                  catch (Exception)
                  {
                      return false;
                  }
              //}
          }
0 Votes 0 ·
VolkVolk-1263 avatar image
0 Votes"
VolkVolk-1263 answered

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)

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.