c# - 当只有读操作时,将 DbContext 作为单例注入(inject)是否可以?

标签 c# .net entity-framework ef-core-3.1

我知道 DbContext 不是线程安全的,加上 DbContext 缓存数据,当多个事务尝试保存/提交自己的更改时可能会导致数据不一致数据库。因此,强烈建议根据请求注入(inject)它(here)。但是我有一个情况,只有读取操作存在(在独立类库中)并且没有事务或创建/更新/删除操作。
我的问题是:在这种情况下将 DbContext 作为单例注入(inject)是否安全?

最佳答案

Entity Framework 开发人员明确表示 DbContext 对于对其执行的任何 操作都不是线程安全的,而不仅仅是写入(添加、保存更改等)操作,您应该如果您不想有一天花几天时间调试神秘的故障,请相信他们。

即使在读取操作时,EF 也可以对其非线程安全的内部结构执行内存中写入操作,您不能确定它在任何给定情况下都不会这样做。例如,来自 documentation处理查询返回的结果集:

If the query is a tracking query, EF checks if the data represents an entity already in the change tracker for the context instance

因此,如果查询正在跟踪查询 - 它会检查当前实例的更改跟踪器,以查找具有相同键的已存在的此类实体,这意味着如果此类实体不存在 - 它会将其放入更改跟踪器中。这是写操作,所以不安全。

你可以说,好吧,那我就用 AsNoTracking() 吧。但是here是另一个问题,关于并发 AsNoTracking 查询 - EF 甚至不允许您执行它们。库维护者说:

Concurrent use of the same DbContext is not possible - and not just for tracked queries. Specifically, a DbContext has an underlying DbConnection to the database, which cannot be used concurrently. There are other various components at work under the hood which don't support multithreading.

However, there's nothing wrong with instantiating several DbContexts and executing queries on them - whether tracking or non-tracking. That should get you the behavior you're looking for. If you run into any further issues don't hesitate to post back.

因此,存在未记录的内部组件,它们不是线程安全的,您无法确定在多个线程中对 DbContext 执行任何操作时,您不会遇到这样的问题组件。

关于c# - 当只有读操作时,将 DbContext 作为单例注入(inject)是否可以?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64568030/

相关文章:

entity-framework - EF6 : Composite Primary Key Field as Foreign Key (ALTER TABLE statement conflicted with the FOREIGN KEY constraint)

asp.net-mvc - Entity Framework 4 不保存我的多对多行

asp.net - .net 项目的结构代码指南

c# - 有什么方法可以自动化 Windows 窗体测试吗?

.net - 新手 .net 问题 - 相当于 $_SESSION ['SERVER_NAME' 的变量]

mysql - 'DbContextOptionsBuilder' 不包含 'UseSqlServer' 的定义

c# - 在 JavaScript 中获取控制权

c# - 在代码优先 Entity Framework 中处理每个用户具有不同值的属性

c# - 我可以在 Docker 中运行现有的 ASP.NET MVC 应用程序而无需在 Linux 中转换为 ASP.NET Core

c# - Safari 将空白表单发送到我的服务器