Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force Entity Framework to always get updated data from the database?

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.

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.

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

like image 668
Saravana Avatar asked Mar 04 '14 16:03

Saravana


People also ask

How does Entity Framework update data?

Update the Records Updating the entity involves getting the entity from the database, make the necessary changes, and then call the SaveChanges to persist the changes in the database. There are two Scenario's that arise, when you update the data to the database.

How do you refresh a table in Entity Framework?

Use the Refresh method: context. Refresh(RefreshMode. StoreWins, yourEntity);

How do you update database changes in Entity Framework first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish. After the update process is finished, the database diagram includes the new MiddleName property.

How do I enable eager loading in Entity Framework?

Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager loading is achieved by use of the Include method. For example, the queries below will load blogs and all the posts related to each blog. Include is an extension method in the System.


2 Answers

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.

like image 77
PlTaylor Avatar answered Oct 11 '22 13:10

PlTaylor


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.

like image 22
Zach Avatar answered Oct 11 '22 13:10

Zach