我应该在 End_Request 事件处理程序中处理给定的 ApplicationDbContext 吗?
Should I dispose given ApplicationDbContext in the End_Request event handler?
我有 Asp.Net 个带有 entity framework 的 Webforms 应用程序。应用程序基于 Visual Studio.
的默认 Web 应用程序构建
应用程序配置为根据 Owin 创建 ApplicationDbContext
上下文(每个请求)。我不会在每次数据库查询后关闭 DbContext,因为那样我就不会利用 EF 提供的所有缓存
所以我使用从当前 Owin 上下文返回的给定 DbContext。但是我没有看到任何一段代码正在处理给定的 DbContext。我应该在 End_Request 事件处理程序中处理给定的 DbContext 吗?或者它是自动处理的? (我猜不是)
DbContext instances should be disposed of (but you'll probably be OK
if they're not) DbContext implements IDisposable. Its instances should
therefore be disposed of as soon as they're not needed anymore. In
practice however, and unless you choose to explicitly manage the
database connection or transaction that the DbContext uses, not
calling DbContext.Dispose() won't cause any issues
正如 EF 团队成员 Diego Vega 所解释的那样
Do I always have to call Dispose() on my DbContext objects? Nope
根据我的经验,我可以补充一点:在过去,我从不担心处理 dbcontext,也从来没有遇到过任何问题。最近我不手动创建它,但我让我的 DI 容器为我实例化它并在需要处理时处理它。
我有 Asp.Net 个带有 entity framework 的 Webforms 应用程序。应用程序基于 Visual Studio.
的默认 Web 应用程序构建应用程序配置为根据 Owin 创建 ApplicationDbContext 上下文(每个请求)。我不会在每次数据库查询后关闭 DbContext,因为那样我就不会利用 EF 提供的所有缓存
所以我使用从当前 Owin 上下文返回的给定 DbContext。但是我没有看到任何一段代码正在处理给定的 DbContext。我应该在 End_Request 事件处理程序中处理给定的 DbContext 吗?或者它是自动处理的? (我猜不是)
DbContext instances should be disposed of (but you'll probably be OK if they're not) DbContext implements IDisposable. Its instances should therefore be disposed of as soon as they're not needed anymore. In practice however, and unless you choose to explicitly manage the database connection or transaction that the DbContext uses, not calling DbContext.Dispose() won't cause any issues
正如 EF 团队成员 Diego Vega 所解释的那样 Do I always have to call Dispose() on my DbContext objects? Nope
根据我的经验,我可以补充一点:在过去,我从不担心处理 dbcontext,也从来没有遇到过任何问题。最近我不手动创建它,但我让我的 DI 容器为我实例化它并在需要处理时处理它。