Ravikumar
Ravikumar

Reputation: 211

Entity Framework core 2.0.3 not giving latest data from Database

I am facing one issue while debugging my application locally.

I just ran my API and UI apps, Doing some operations as follows.

Using dbContext getting data from one of the tables, When we are fetching the data initially getting fresh data from the table. Later I have updated data in one of the columns from DB directly with update script.

When I Fetching the data gain from the same table using dbcontext, getting older data. till now I did not stop the API. If I stop and start API again getting updated values from the same table using dbContext.

Not sure what is causing this issue.

Upvotes: 2

Views: 895

Answers (2)

Sabel
Sabel

Reputation: 589

I fixed a similar issue by shifting a dependency injection service from singleton to transient.

Upvotes: 0

Ravikumar
Ravikumar

Reputation: 211

I found the issue, this is happening because of Dependency Injections object scops.

the service which is using dbContext is a singleton object, due to which dbContext is not refreshed(not creating) every time in request. As we updated the data in the database directly with update script, so dbcontext doesn't have the track of changes, and dbContect is no refreshing every time data getting from cached context instead of the actual database.

Upvotes: 2

Related Questions