Why does this occur when the AccountApiController.Register() method is called?
"Message":"An error has occurred.",
"ExceptionMessage":"The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.",
--- End of stack trace from previous location where exception was thrown
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter .HandleNonSuccessAndDebuggerNotification(Task > task)
We had a difficulty since we weren't following the factory pattern, which was MS suggests.
You can use Factory implementation to get an instance of UserManager from the OWIN context. ... This is a recommended way of getting an instance of UserManager per request for the application.
As a result, several requests and therefore threads shared a DbContext, causing "the same context instance to be used by numerous threads concurrently."
The next sentence is accurate. For each call to the UserManagerFactory method, a fresh instance of MyDbContext is created.
UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>(new MyDbContext()));
The next statement is inaccurate. Although it has a similar appearance, it does not produce a new instance with each call to the UserManagerFactory. Because we were utilizing it, our website crashed.
var userStore = new UserStore<IdentityUser>(new MyDbContext()); var userManager = new UserManager<IdentityUser>(userStore); UserManagerFactory = () => userManager;
This issue can also happen when there are
. Verify if
is true (no typo etc.).