问题描述
所以我目前正在使用实体框架的ASP.NET MVC Web应用程序上工作,我也正在使用Ninject进行依赖项注入.
so I am currently working on an ASP.NET MVC web application that uses Entity Framework, I'm also using Ninject for Dependency Injection.
因此,目前基本上,这就是我向Ninject注册我的DbContext和服务的方式.
So basically, at the moment, this is how I register my DbContext and Services with Ninject.
kernel.Bind<DbContext>().To<MyApplicationContext>().InSingletonScope();
kernel.Bind<IAccountService>().To<AccountService>().InSingletonScope();
kernel.Bind<IRegionService>().To<RegionService>().InSingletonScope();
kernel.Bind<IRoleService>().To<RoleService>().InSingletonScope();
我向InSingletonScope
注册了它们,这意味着它们将仅创建一次并在应用程序的整个生命周期内使用(至少以我的理解).
I register them with InSingletonScope
, which means that they will only be created once and used throughout the lifetime of the application (at least how I understand it).
控制器:
private IAccountService _accountService;
public MemberController(IAccountService accountService)
{
_accountService = accountService;
}
但是,我深感这种单例作用域将在我的Web应用程序中引起问题,尤其是在实体框架的上下文中,因为它是单例的.
However, I have a deep feeling that this singleton scope will cause problem in my web application especially for the Entity Framework's context, due to it being singleton.
因此,我已经遇到了一个小问题,如果我使用SQL Management Studio手动更新数据库,则在我重新启动应用程序之前,Web应用程序的实体框架中的数据将不会更新(似乎是某种缓存机制). EF).
I am already facing a minor issue due to this, if I manually update the database using SQL Management Studio, the data in my web application's Entity Framework wouldn't update until I restart the application (seems to be some caching mechanism in EF).
-
但是,如果我删除了InSingletonScope
,我会从EF中随机得到错误,说:
However, if I remove the InSingletonScope
, I will randomly get errors from EF saying that:
我理解为什么会这样,因为由AccountService
初始化的DbContext可能不同于RegionService
.但是我不知道如何解决这个问题.
I understand why this happens because the DbContext initialized by AccountService
could be different from say, RegionService
. But I have no idea how I can resolve this.
我对依赖注入的理解仍然非常有限,所以任何人都可以请教吗?
My understanding of Dependency Injection is still very limited, so can anybody please advice?
-
我已经尝试将所有进样更改为InRequestScope
,但是我仍然得到
I've tried changing to InRequestScope
for all the injections, but I'm still getting
当尝试从我的应用程序中的另一服务插入带有相关对象(外键)的新实体时.这意味着他们仍在使用其他DbContext,这是怎么回事?!
When trying to insert a new entity with related object (foreign key) from another service in my application. That means they are still using a different DbContext, what is happening?!
最终好吧,我发现了问题,这是我的缓存机制缓存了先前的请求,导致所有后续请求的关系出现问题.
FINAL Ok I've found the problem, it was my caching mechanism that was caching a previous request, causing the relationship issue on all subsequent request.
推荐答案
我终于设法使用InRequestScope
而不是InSingletonScope
解决了此问题.
I've finally managed to resolve this issue by using InRequestScope
instead of InSingletonScope
.
最初,由于我在服务层上已有的缓存机制,在更改为InRequestScope
之后,我仍然面临着同样的问题.
Initially, I was still facing the same problem after changing to InRequestScope
because of my existing caching mechanism on my services layer.
因此,所有后续请求都使用了最初缓存的实体对象,这就是为什么我从EF收到多个实例错误的原因.
Thus, all subsequent requests were using the initially-cached entity object, that was why I was getting multiple instances error from EF.
-
如果您仍然面对着
更改为InRequestScope
后出现
错误,请确保您的实体没有以某种方式缓存或存储以供后续HTTP请求使用.
error after changing to InRequestScope
, make sure your entities are not somehow cached or stored for subsequent HTTP requests uses.
这篇关于EF的DbContext的单例范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!