简体   繁体   English

当我在 EF Core 中执行 ToList() 时,是否释放了 DbContext

[英]Is the DbContext disposed when I do a ToList() in EF Core

I read that, with EF Core , it is not obligated to use a using statement with the DBContext to dispose of it (or to call the de Dispose method directly).我读到,对于EF Core ,没有义务使用带有DBContext的 using 语句来处理它(或直接调用 de Dispose 方法)。 The garbage collector will do it.垃圾收集器会这样做。 I also read that when you read the objects you've retrieved, they should dispose of too, but I am not sure if I understand correctly.我还读到,当您阅读检索到的对象时,它们也应该处理掉,但我不确定我是否理解正确。 Does it mean that if I do a toList() on a collection return by my DbContext, that my context will be disposed of at this moment?这是否意味着如果我对 DbContext 的集合返回执行toList() ,我的上下文将在此时被处理掉? I have difficulties finding this specific answer on Google.我很难在谷歌上找到这个特定的答案。

The resource management of Entity Framework does not work in that manner. Entity Framework 的资源管理不能以这种方式工作。 Entity Framework explicitly defines the lifetime when an instance created until the instance is disposed of.实体框架显式定义了实例创建时的生命周期,直到实例被处置。 When the framework was developed, the fundamental purpose was to exist within a single unit-of-work which is why it is well suited for web applications, it can be used elsewhere- but the fundamental usage fit.在开发框架时,基本目的是存在于单个工作单元中,这就是为什么它非常适合 Web 应用程序,它可以在其他地方使用——但基本用途适合。

How you configure and scope your service will define the lifetime of the instance.您如何配置和确定服务范围将定义实例的生命周期。 But it should adhere to the following principals:但它应遵守以下原则:

Typical unit-of-work would involve the creation of an instance, the tracking of the entity instances defined on the context, returned from query, added or attached to the context, change being made to the tracked entities.典型的工作单元将涉及创建实例、跟踪在上下文中定义的实体实例、从查询返回、添加或附加到上下文、对跟踪的实体进行更改。 Then save those changes made and write them to the database.然后保存所做的更改并将它们写入数据库。 Then disposal of the instance.然后处置实例。

So if you leverage a service such as:因此,如果您利用以下服务:

public class CustomerAccountService : ICustomerAccountService
{
     private bool disposed = false;
     private readonly ICustomerRepository repository;

     public CustomerAccountService(ICustomerRepository repository) => this.repository = repository;

    // Additional logic would go here, including the implementation of the repository which will hold our DbContext.
}

When the CustomerAccountService is no longer scoped, then it would begin to clean dispose and clean up the Entity Framework instance as the lifetime has expired once the service.CustomerAccountService不再限定范围时,它将开始清理处置和清理实体框架实例,因为一旦服务的生命周期已过期。 Keep in mind that the Dependency Injection container and your configuration will impact how this will be impacted.请记住,依赖注入容器和您的配置将影响它的影响方式。 If you can, you should always add an IDispose that correctly reallocates those resources.如果可以,您应该始终添加正确重新分配这些资源的IDispose

The documentation explicitly states:该文档明确指出:

It is very important to dispose the DbContext after use.使用后处理 DbContext 非常重要。 This ensures both that any unmanaged resources are freed, and that any events or other hooks are unregistered so as to prevent memory leaks in case the instance remains referenced.这可以确保释放任何非托管资源,并确保取消注册任何事件或其他挂钩,以防止在实例保持引用的情况下发生内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF Core - 从多个表顺序查询时释放 DbContext - EF Core - DbContext disposed when query from multiple tables sequentially 尝试在 Startup ExceptionHandler 中访问 EF Core DbContext 时“无法访问已释放的上下文” - "Cannot access a disposed context" when trying to access EF Core DbContext in Startup ExceptionHandler 将DBContext放入IMemoryCache(.NET Core / EF Core)之后为什么要处理它 - Why is DBContext is disposed after putting it in IMemoryCache (.NET Core / EF Core) 为什么在EF Core中的每个api调用之后都会自动丢弃DbContext? - Why DbContext autometically disposed after every api call in EF Core? 在异步方法完成之前处理EF Core DbContext - EF Core DbContext being disposed of before Async methods are complete 我是否需要手动关闭 EF 核心 Dbcontext 连接 - Do I need to close EF core Dbcontext connection manually 为什么我需要ToList()来避免处置上下文错误? - Why do I need a ToList() to avoid disposed context errors? DbContext 实例何时在 ASP.NET Core 5 中得到处置 - When does a DbContext instance get disposed in ASP.NET Core 5 asp.net core dbcontext 在计时器结束时被处理 - asp.net core dbcontext is disposed when timer elapsed 我何时需要在EF的DbContext中指定DbSet? - When do I need to specify DbSet in EF's DbContext?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM