Click here to Skip to main content
16,016,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We have an MVC project which is written in c#. We are using entity framework to connect with sql db.
I use repository pattern with service implementation in our project. Manual db updates do not reflect to get queries of repository. How can I force ef get latest data?

C#
private readonly ICompanyService companyService;
        public HomeController(ICompanyService companyService)
        {
            this.companyService = companyService;
        }
companyService.Get(id) -> this doesnt return updated company

I updated company from db while app is running.
If I call the company with this method, updated data is not returned.

It only works with this way;
C#
ServiceFactory.Services.CompanyService.Get(id)


ServiceFactory-> CompanyService is
C#
private ICompanyService _companyService;
        public ICompanyService CompanyService
        {
            get
            {
                return _companyService ?? (_companyService =
                    new CompanyService(dbFactory, companyRepository, companyCustomerRepository,
                    regionCountryRepository, countryRepository, zoneRepository, unitOfWork));
            }
        }


What I have tried:

I can only do this without using service implementation
Posted
Updated 7-Dec-18 2:05am
v7
Comments
CHill60 7-Dec-18 4:08am    
How can we help you if we don't know what language you have coded in, what code you have written, what database you are using?

You have given us absolutely no useful information!
kubibay 7-Dec-18 4:39am    
i added details, thx
I supposed this maybe a known issue and there may be a quick answer for ti.
F-ES Sitecore 7-Dec-18 5:26am    
It's impossible to say from the code you've posted. Use the debugger to step through your code line by line to see why you are not getting the new data from the database. My guess would be that you're caching the data somewhere and returning from that cache rather than the database.

1 solution

It sounds like you're using a single DbContext instance across the entire application.

Once an entity has been loaded by a DbContext instance, it will be cached for the lifetime of that instance. Any attempt to read that entity from that instance will return the cached value, ignoring any changes in the database, unless you manually force a refresh via the change tracker.

Entity Framework Cache Busting | Codethug[^]

For a web application, you should be using a new DbContext instance per request. How you do this will depend on which IoC container you're using.
 
Share this answer
 
Comments
kubibay 9-Dec-18 23:42pm    
Yes, I use SingleInstance of dbfactory,repositories and services.
But when I use InstancePerRequest then it increases 100ms for every request.
Since we don't want request durations to increase, I gave up using service implementation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900