"不能被创建,而模型所用,语境;例外与ASP.NET身份 [英] "Context cannot be used while the model is being created" exception with ASP.NET Identity

查看:147
本文介绍了"不能被创建,而模型所用,语境;例外与ASP.NET身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是怎么回事,当我们做出了AccountApiController.Register()方法调用?


  • 什么是试图使用上下文?

  • 什么是试图创建上下文?

  • 我们如何避免这种情况?

  • 我们该如何调试呢?


  

{


  
  

消息:发生错误


  
  

ExceptionMessage:的上下文,不能使用,而该模型是
  正在创建。如果使用此背景下可能会引发异常
  该OnModelCreating方法里面,或者相同的上下文实例
  由多个线程同时访问。需要注意的是实例成员
  的DbContext和相关类都不能保证是线程
  安全,


  
  

ExceptionType:System.InvalidOperationException


  
  

堆栈跟踪:


  
  

在System.Web.Http.ApiController.d__1.MoveNext()


  
  

---从previous位置堆栈跟踪,其中引发异常的结束


  
  

在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)


  
  

在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务>任务)


  
  

在System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()


  
  

}



解决方案

的问题是,我们没有使用工厂模式的<一个href=\"http://blogs.msdn.com/b/webdev/archive/2013/12/20/announcing-$p$pview-of-microsoft-aspnet-identity-2-0-0-alpha1.aspx?PageIndex=2\">MS建议。


  

您可以使用工厂实现来获得的UserManager的实例
  从OWIN上下文。 ...这是得到的一个实例的推荐方式
  每个请求的应用程序的UserManager。


其结果是,相同的上下文实例由多个线程同时访问,因为几个请求,从而线程共享一个的DbContext。

这下面是正确的。这对于每次调用UserManagerFactory函数创建MyDbContext的新实例。

  UserManagerFactory
=()=&GT;新的UserManager&所述; IdentityUser&GT;(新UserStore&所述; IdentityUser&GT;(新MyDbContext()));

以下是不正确。它看起来相似,但每次调用UserManagerFactory不创建一个新的实例。这是我们正在使用,ERGO我们的网站打破了。

  VAR userStore =新UserStore&LT; IdentityUser&GT;(新MyDbContext());
VAR的UserManager =新的UserManager&LT; IdentityUser&GT;(userStore);
UserManagerFactory =()=&GT;的UserManager;

Why is this happening when we make a call to the AccountApiController.Register() method?

  • 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.",

"ExceptionType":"System.InvalidOperationException",

"StackTrace":"

at 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)

at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task > task)

at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()"

}

解决方案

The problem was that we were NOT using the factory pattern that MS recommends.

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, "the same context instance is accessed by multiple threads concurrently," because several requests and thus threads shared a DbContext.

This following is correct. It creates a new instance of MyDbContext for each call to the UserManagerFactory function.

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.

var userStore = new UserStore<IdentityUser>(new MyDbContext());                    
var userManager = new UserManager<IdentityUser>(userStore);
UserManagerFactory = () => userManager;

这篇关于&QUOT;不能被创建,而模型所用,语境;例外与ASP.NET身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆