EF 的 DbContext 的单例范围 [英] Singleton Scope for EF's DbContext

查看:22
本文介绍了EF 的 DbContext 的单例范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我目前正在开发一个使用实体框架的 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:

一个实体对象不能被多个 IEntityChangeTracker 实例引用

An entity object cannot be referenced by multiple instances of IEntityChangeTracker

我理解为什么会发生这种情况,因为由 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

一个实体对象不能被多个 IEntityChangeTracker 实例引用

An entity object cannot be referenced by multiple instances of IEntityChangeTracker

尝试从我的应用程序中的另一个服务中插入具有相关对象(外键)的新实体时.这意味着他们仍在使用不同的 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.

--

如果您仍然面临

一个实体对象不能被多个 IEntityChangeTracker 实例引用

An entity object cannot be referenced by multiple instances of IEntityChangeTracker

更改为 InRequestScope 后出错,请确保您的实体没有以某种方式缓存或存储以供后续 HTTP 请求使用.

error after changing to InRequestScope, make sure your entities are not somehow cached or stored for subsequent HTTP requests uses.

这篇关于EF 的 DbContext 的单例范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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