Is it mandatory for DbContext to be injected .InPerRequestScope in ASP.NET application?

*爱你&永不变心* 提交于 2019-12-12 13:25:25

问题


I have been using Ninject IoC container in my ASP.NET MVC3 portal. Whenever I've been injecting Entity Framework DbContext in PerThread scope, my data wasn't consistent, changes would not get displayd for some time after I've made changes to Entities, etc.

After I've switched the IoC configuration to resolve a fresh copy of my DbContext instance for each request (PerRequestScope()), all the problems were gone.

So is it absolutely mandatory to use PerRequest injection strategy in MVC3 applications?


回答1:


Yes, it is mandatory.

Your problem works like this:

  • Thread A loads an entity
  • Thread B modifies that entity
  • The next request to Thread A uses the cached entity from the first request, ignoring the changes


来源:https://stackoverflow.com/questions/9122480/is-it-mandatory-for-dbcontext-to-be-injected-inperrequestscope-in-asp-net-appli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!