gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:34:09 24 4
gpt4 key购买 nike

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

最佳答案

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

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

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

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

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

Concurrent use of the same DbContext is not possible - and not justfor tracked queries. Specifically, a DbContext has an underlyingDbConnection to the database, which cannot be used concurrently. Thereare other various components at work under the hood which don'tsupport multithreading.

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

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com