Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View's in Entity Framework won't update

I have a view which includes data from multiple tables.

When I call that view, the results are loaded into the memory and stored. After I make some changes to the other tables which should effect the view, the view don't know anything about the changes.

So when I call that view again, for example a Get() method, EF returns the values of the stored data.

Of course I want the updated data. How can I force the view to get the data from DB and not from memory? Or is there a better strategy?

Better if I can make the view aware of the changes being made. Is this achievable with entity configuration, maybe by using HasRequired() method to map FK's?

EDIT: I am using repository and unit of work pattern. So I am not creating and disposing a new context each time. Please consider this.

like image 503
Mert Akcakaya Avatar asked Jan 15 '23 22:01

Mert Akcakaya


1 Answers

When you execute the Linq query on the view use AsNoTracking() extension method. This will force EF to always use data from database instead of memory:

var result = from x in context.ViewSet.AsNoTracking()
             select x;

EF will not handle any automatic data refresh for you.

like image 73
Ladislav Mrnka Avatar answered Jan 25 '23 12:01

Ladislav Mrnka