如何强制实体框架始终从数据库获取更新的数据?

时间:2022-06-14 09:51:59

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.

我用EntityFramework。扩展库以执行批处理更新。唯一的问题是EF没有跟踪库执行的批处理更新。因此,当我再次查询DbContext时,它不会返回更新的实体。

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.

我发现在查询时使用AsNoTracking()方法会禁用跟踪并从数据库中获取新的数据。但是,由于EF没有跟踪使用AsNoTracking()查询的实体,所以我无法对查询的数据执行任何更新。

Is there any way to force EF to get the latest data while tracking changes?

有没有办法迫使EF在跟踪变化的同时获取最新的数据?

5 个解决方案

#1


95  

Please try this to refresh a single entity:

请尝试刷新一个实体:

Context.Entry<T>(entity).Reload()

Edit: To get fresh data for a collection of entities is worth trying to dispose the DbContext instance after each request.

编辑:为了获得实体集合的新数据,值得尝试在每个请求之后处理DbContext实例。

#2


3  

I stumbled upon this question while searching for a solution to a problem I was having where the navigation properties were not populating after updating the entity. Whenever I attempted to reload the entity from the database, it would grab the entry from the local store instead which would not populate the navigation properties via lazy loading. Instead of destroying the context and recreating one, I found this allowed me to get fresh data with the proxies working:

我在寻找更新实体后导航属性没有填充的问题的解决方案时偶然发现了这个问题。每当我尝试从数据库中重新加载实体时,它都会从本地存储中获取条目,而不是通过延迟加载来填充导航属性。我没有破坏上下文并重新创建上下文,而是发现这使我能够在代理工作的情况下获得新的数据:

_db.Entry(entity).State = EntityState.Detached;

The logic behind it was - my update attached the entity so it would track changes to it. This adds it to the local store. Thereafter, any attempts to retrieve the entity with functional proxies would result in it grabbing the local one instead of going out to the db and returning a fresh, proxy-enabled entity. I tried the reload option above, which does refresh the object from the database, but that doesn't give you the proxied object with lazy-loading. I tried doing a Find(id), Where(t => t.Id = id), First(t => t.Id = id). Finally, I checked the available states that were provided and saw there was a "Detached" state. Eureka! Hope this helps someone.

它背后的逻辑是——我的更新附加了实体,这样它就可以跟踪对它的更改。这将它添加到本地商店。此后,任何使用函数代理检索实体的尝试都会导致它获取本地实体,而不是走到db并返回一个新的、代理支持的实体。我尝试了上面的reload选项,它确实从数据库中刷新了对象,但是这并没有为您提供具有延迟加载的proxied对象。我尝试做一个Find(id),其中(t => t)Id = Id),首先(t => t)。Id = Id)。最后,我检查了提供的可用状态,发现有一个“分离”状态。尤里卡!希望这可以帮助别人。

#3


1  

Making the code run on the same context will not yield you updated entities. It will only append new entities created in the database between runs. EF force reload can be done like this:

让代码在相同的上下文中运行不会产生更新的实体。它只会在运行之间添加在数据库中创建的新实体。EF力重载可以这样做:

ObjectQuery _query = Entity.MyEntity;
_query.MergeOption = MergeOption.OverwriteChanges;
var myEntity = _query.Where(x => x.Id > 0).ToList();

#4


1  

I declared the entity variable, without assignment, as part of the class. This allowed me to dispose of an instance without losing the variable for reference by other methods. I just came across this so it doesn't have alot of runtime under it's belt, but so far it seems to be working fine.

我声明实体变量,没有赋值,作为类的一部分。这允许我在不丢失变量以供其他方法引用的情况下处理实例。我刚遇到这个,所以它没有很多运行时在它的腰带下,但到目前为止它似乎运行得很好。

public partial class frmMyForm
{
    private My_Entities db;

    public frmMyForm()
    {
        InitializeComponent();
    }

    private void SomeControl_Click(object sender, EventArgs e)
    {
        db.SaveChanges();
    db.Dispose();
        db = new My_Entities();
    ...  More Code using db ...
}

#5


0  

For me ... I access my DbContext like this:

对我来说……我像这样访问我的DbContext:

_viewModel.Repo.Context

To force EF to hit the database I do this:

为了迫使EF攻击数据库,我这样做:

_viewModel.Repo.Context = new NewDispatchContext();

Overwriting the current DbContext with a new instance. Then the next time I use my data services they get the data from the database.

用一个新实例覆盖当前的DbContext。然后下次我使用我的数据服务时,他们会从数据库中获取数据。

#1


95  

Please try this to refresh a single entity:

请尝试刷新一个实体:

Context.Entry<T>(entity).Reload()

Edit: To get fresh data for a collection of entities is worth trying to dispose the DbContext instance after each request.

编辑:为了获得实体集合的新数据,值得尝试在每个请求之后处理DbContext实例。

#2


3  

I stumbled upon this question while searching for a solution to a problem I was having where the navigation properties were not populating after updating the entity. Whenever I attempted to reload the entity from the database, it would grab the entry from the local store instead which would not populate the navigation properties via lazy loading. Instead of destroying the context and recreating one, I found this allowed me to get fresh data with the proxies working:

我在寻找更新实体后导航属性没有填充的问题的解决方案时偶然发现了这个问题。每当我尝试从数据库中重新加载实体时,它都会从本地存储中获取条目,而不是通过延迟加载来填充导航属性。我没有破坏上下文并重新创建上下文,而是发现这使我能够在代理工作的情况下获得新的数据:

_db.Entry(entity).State = EntityState.Detached;

The logic behind it was - my update attached the entity so it would track changes to it. This adds it to the local store. Thereafter, any attempts to retrieve the entity with functional proxies would result in it grabbing the local one instead of going out to the db and returning a fresh, proxy-enabled entity. I tried the reload option above, which does refresh the object from the database, but that doesn't give you the proxied object with lazy-loading. I tried doing a Find(id), Where(t => t.Id = id), First(t => t.Id = id). Finally, I checked the available states that were provided and saw there was a "Detached" state. Eureka! Hope this helps someone.

它背后的逻辑是——我的更新附加了实体,这样它就可以跟踪对它的更改。这将它添加到本地商店。此后,任何使用函数代理检索实体的尝试都会导致它获取本地实体,而不是走到db并返回一个新的、代理支持的实体。我尝试了上面的reload选项,它确实从数据库中刷新了对象,但是这并没有为您提供具有延迟加载的proxied对象。我尝试做一个Find(id),其中(t => t)Id = Id),首先(t => t)。Id = Id)。最后,我检查了提供的可用状态,发现有一个“分离”状态。尤里卡!希望这可以帮助别人。

#3


1  

Making the code run on the same context will not yield you updated entities. It will only append new entities created in the database between runs. EF force reload can be done like this:

让代码在相同的上下文中运行不会产生更新的实体。它只会在运行之间添加在数据库中创建的新实体。EF力重载可以这样做:

ObjectQuery _query = Entity.MyEntity;
_query.MergeOption = MergeOption.OverwriteChanges;
var myEntity = _query.Where(x => x.Id > 0).ToList();

#4


1  

I declared the entity variable, without assignment, as part of the class. This allowed me to dispose of an instance without losing the variable for reference by other methods. I just came across this so it doesn't have alot of runtime under it's belt, but so far it seems to be working fine.

我声明实体变量,没有赋值,作为类的一部分。这允许我在不丢失变量以供其他方法引用的情况下处理实例。我刚遇到这个,所以它没有很多运行时在它的腰带下,但到目前为止它似乎运行得很好。

public partial class frmMyForm
{
    private My_Entities db;

    public frmMyForm()
    {
        InitializeComponent();
    }

    private void SomeControl_Click(object sender, EventArgs e)
    {
        db.SaveChanges();
    db.Dispose();
        db = new My_Entities();
    ...  More Code using db ...
}

#5


0  

For me ... I access my DbContext like this:

对我来说……我像这样访问我的DbContext:

_viewModel.Repo.Context

To force EF to hit the database I do this:

为了迫使EF攻击数据库,我这样做:

_viewModel.Repo.Context = new NewDispatchContext();

Overwriting the current DbContext with a new instance. Then the next time I use my data services they get the data from the database.

用一个新实例覆盖当前的DbContext。然后下次我使用我的数据服务时,他们会从数据库中获取数据。