Why is this happening when we make a call to the AccountApiController.Register() method?
当我们调用AccountApiController.Register()方法时,为什么会发生这种情况?
- what is trying to use the context?
- 什么是试图使用上下文?
- what is trying to create the context?
- 什么是创建上下文?
- how do we avoid this?
- 我们如何避免这种情况?
- how do we debug this?
- 我们如何调试它?
"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.",
“ExceptionMessage”:“在创建模型时不能使用上下文。如果在onmodelcreate方法中使用上下文,或者并发地访问同一上下文实例,则可能引发此异常。注意,DbContext和相关类的实例成员不能保证是线程安全的。
"ExceptionType":"System.InvalidOperationException",
:“ExceptionType System.InvalidOperationException”,
"StackTrace":"
“加”:“
at System.Web.Http.ApiController.d__1.MoveNext()
在System.Web.Http.ApiController.d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown
---从先前抛出异常的位置结束堆栈跟踪
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
在System.Runtime.CompilerServices.TaskAwaiter。ThrowForNonSuccess(工作任务)
at System.Runtime.CompilerServices.TaskAwaiter .HandleNonSuccessAndDebuggerNotification(Task > task)
在System.Runtime.CompilerServices。TaskAwaiter .HandleNonSuccessAndDebuggerNotification(任务>)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()"
在System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()
5 个解决方案
#1
22
The problem was that we were NOT using the factory pattern that MS recommends.
问题是我们没有使用MS推荐的工厂模式。
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.
您可以使用工厂实现从OWIN上下文中获取UserManager实例。这是为应用程序获取每个请求的UserManager实例的推荐方法。
As a result, "the same context instance is accessed by multiple threads concurrently," because several requests and thus threads shared a DbContext.
因此,“同一个上下文实例被多个线程同时访问”,因为多个请求和线程共享一个DbContext。
This following is correct. It creates a new instance of MyDbContext for each call to the UserManagerFactory function.
这下面是正确的。它为每个调用UserManagerFactory函数创建了一个MyDbContext的新实例。
UserManagerFactory
= () => new UserManager<IdentityUser>(new UserStore<IdentityUser>(new MyDbContext()));
The following is incorrect. It look similar but does not create a new instance for each call to UserManagerFactory. It is what we were using, ergo our site broke.
以下是不正确的。它看起来很相似,但是没有为每个调用UserManagerFactory创建一个新的实例。这就是我们使用的,所以我们的网站坏了。
var userStore = new UserStore<IdentityUser>(new MyDbContext());
var userManager = new UserManager<IdentityUser>(userStore);
UserManagerFactory = () => userManager;
#2
22
This error can also occur in case of incorrect connectionString
. Check if connectionString
is valid (no typo etc.).
如果connectionString错误,也会出现此错误。检查connectionString是否有效(没有输入错误等)。
#3
1
Do you override the OnModelCreating method? If so, can you share it or the whole context class?
是否覆盖onmodelcreation方法?如果有,你能分享它或整个上下文类吗?
If not, you should pay attention to the following in the error message
如果没有,您应该注意错误消息中的以下内容
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.
或者如果同一上下文实例被多个线程同时访问。注意,DbContext和相关类的实例成员不能保证是线程安全的。
If that doesn't help, do you use an unchanged Web API project which is created by Visual Studio?
如果这没有帮助,您是否使用由Visual Studio创建的未更改的Web API项目?
#4
0
If you are getting records from Table/View make sure you have sufficient access on the DB objects.
如果要从表/视图获取记录,请确保对DB对象具有足够的访问权限。
I had the same problem and i resolved it by executing
我也有同样的问题,我通过执行来解决
sp_change_users_login [ @Action = ] 'action' [ , [ @UserNamePattern = ] 'user' ] [ , [ @LoginName = ] 'login' ] [ , [ @Password = ] 'password' ] [;]
sp_change_users_login 'update_one' 'dbuser' 'dblogin'
找到更多的代码片段和细节
#5
0
I'm using EF Code First in a multi-layered architecture with MySQL and had the same exception and using the following line inside the DBContext Class constructure works:
我在使用MySQL的多层架构时首先使用了EF代码,并有相同的例外,在DBContext类构造工程中使用了如下一行:
Database.SetInitializer<EntitiesName>(null);
#1
22
The problem was that we were NOT using the factory pattern that MS recommends.
问题是我们没有使用MS推荐的工厂模式。
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.
您可以使用工厂实现从OWIN上下文中获取UserManager实例。这是为应用程序获取每个请求的UserManager实例的推荐方法。
As a result, "the same context instance is accessed by multiple threads concurrently," because several requests and thus threads shared a DbContext.
因此,“同一个上下文实例被多个线程同时访问”,因为多个请求和线程共享一个DbContext。
This following is correct. It creates a new instance of MyDbContext for each call to the UserManagerFactory function.
这下面是正确的。它为每个调用UserManagerFactory函数创建了一个MyDbContext的新实例。
UserManagerFactory
= () => new UserManager<IdentityUser>(new UserStore<IdentityUser>(new MyDbContext()));
The following is incorrect. It look similar but does not create a new instance for each call to UserManagerFactory. It is what we were using, ergo our site broke.
以下是不正确的。它看起来很相似,但是没有为每个调用UserManagerFactory创建一个新的实例。这就是我们使用的,所以我们的网站坏了。
var userStore = new UserStore<IdentityUser>(new MyDbContext());
var userManager = new UserManager<IdentityUser>(userStore);
UserManagerFactory = () => userManager;
#2
22
This error can also occur in case of incorrect connectionString
. Check if connectionString
is valid (no typo etc.).
如果connectionString错误,也会出现此错误。检查connectionString是否有效(没有输入错误等)。
#3
1
Do you override the OnModelCreating method? If so, can you share it or the whole context class?
是否覆盖onmodelcreation方法?如果有,你能分享它或整个上下文类吗?
If not, you should pay attention to the following in the error message
如果没有,您应该注意错误消息中的以下内容
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.
或者如果同一上下文实例被多个线程同时访问。注意,DbContext和相关类的实例成员不能保证是线程安全的。
If that doesn't help, do you use an unchanged Web API project which is created by Visual Studio?
如果这没有帮助,您是否使用由Visual Studio创建的未更改的Web API项目?
#4
0
If you are getting records from Table/View make sure you have sufficient access on the DB objects.
如果要从表/视图获取记录,请确保对DB对象具有足够的访问权限。
I had the same problem and i resolved it by executing
我也有同样的问题,我通过执行来解决
sp_change_users_login [ @Action = ] 'action' [ , [ @UserNamePattern = ] 'user' ] [ , [ @LoginName = ] 'login' ] [ , [ @Password = ] 'password' ] [;]
sp_change_users_login 'update_one' 'dbuser' 'dblogin'
找到更多的代码片段和细节
#5
0
I'm using EF Code First in a multi-layered architecture with MySQL and had the same exception and using the following line inside the DBContext Class constructure works:
我在使用MySQL的多层架构时首先使用了EF代码,并有相同的例外,在DBContext类构造工程中使用了如下一行:
Database.SetInitializer<EntitiesName>(null);