How to force Entity Framework to Always Load Fresh copy of Data from the Database

Solution:

There are many solution to achieve this result.

The most popular and easiest one is to dispose the existing context object and create new one

entities.Dispose();
entities = new Entities(); // DbContext

but it is not optimal you are disposing whole context which could be very expensive.

Optimal Solution:

Db.Entry(item).Reload(); // Db here means DbContext

this will only load the entity (item) you passed from the database and if you changed the item before reload, after reload the change will be overwritten by the values coming from database.

For more information Please check

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: