Refreshing entities

Because of the first level cache, reloading an entity with modified database values will not refresh it.

Problem

Entity Framework Core, like other Object-Relational Mappers, uses something called a First Level Cache (also known as Identity Map) to keep track of the entities that it knows about. These are entities that were loaded from the database or ones that have been marked for persistence. This, in general, can be regarded as an optimization: when Entity Framework loads the same record over and over again, it does not need to instantiate the entity's class and hydrate it with the values coming from the database.

The problem is, what if the entity's record changes in the database and we wish to refresh the ones we have? For ...

Get Entity Framework Core Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.