如何强制实体框架总是从数据库中获取更新的数据? [英] How to force Entity Framework to always get updated data from the database?
问题描述
我使用 EntityFramework.Extended 库执行批量更新。唯一的问题是EF不跟踪由库进行批量更新。所以,当我查询的DbContext
再次,它不返回更新的实体。
I am using EntityFramework.Extended library to perform batch updates. The only problem is EF does not keep track of the batch updates performed by the library. So when I query the DbContext
again it does not return the updated entities.
我发现,使用 AsNoTracking()
方法,而查询禁用跟踪,并从数据库中获取新的数据。然而,因为EF没有跟上 AsNoTracking()
查询的实体的轨道,我不能够在查询的数据进行任何更新。
I found that using AsNoTracking()
method while querying disables the tracking and gets fresh data from the database. However, since EF does not keep track of the entities queried with AsNoTracking()
, I am not able to perform any update on the queried data.
有没有办法强迫EF同时跟踪更改获取最新数据?
Is there any way to force EF to get the latest data while tracking changes?
推荐答案
请试试这个刷新一个单一的实体:
Please try this to refresh a single entity:
Context.Entry<T>(entity).Reload()
编辑:
为了获得新的数据实体的集合是值得尝试的每个请求后处置的DbContext
实例。
这篇关于如何强制实体框架总是从数据库中获取更新的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!