ReRoute
ReRoute

Reputation: 317

How to refresh the DbContext with a Database first approach

Problem: We have an application that is potentially a multi-user environment. Because of Entity's caching, we are unable to see another users changes in the database when/after it happens. I am not looking to continuously poll the database. I would be okay if the user could click a "Refresh" button and have the context grab the new information from the database.

Things of note: -We are utilizing a cache with a decently long life-time, and would like to keep that. -Solely looking for a way to refresh the context with the latest values from the database.

Any insight would be greatly appreciated. I can provide a code sample upon request.

Thanks

Upvotes: 0

Views: 410

Answers (2)

tobypls
tobypls

Reputation: 849

If you want to reload a single entity from the database, you can do the following:

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

Upvotes: 1

TanvirArjel
TanvirArjel

Reputation: 32069

You can do as follows:

_dbContext.ChangeTracker.Entries().Where(e => e.Entity != null).ToList()
            .ForEach(e => e.State = EntityState.Detached);

Upvotes: 0

Related Questions