Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to... Display Data from Database

I've got an Application which consists of 2 parts at the moment

  1. A Viewer that receives data from a database using EF
  2. A Service that manipulates data from the database at runtime.

The logic behind the scenes includes some projects such as repositories - data access is realized with a unit of work. The Viewer itself is a WPF-Form with an underlying ViewModel.

The ViewModel contains an ObservableCollection which is the datasource of my Viewer.

Now the question is - How am I able to retrieve the database-data every few minutes? I'm aware of the following two problems:

  1. It's not the latest data my Repository is "loading" - does EF "smart" stuff and retrieves data from the local cache? If so, how can I force EF to load the data from the database?
  2. Re-Setting the whole ObservableCollection or adding / removing entities from another thread / backgroundworker (with invokation) is not possible. How am I supposed to solve this?

I will add some of my code if needed but at the moment I don't think that this would help at all.

Edit:

public IEnumerable<Request> GetAllUnResolvedRequests() {
        return AccessContext.Requests.Where(o => !o.IsResolved);
    }

This piece of code won't get the latest data - I edit some rows manually (set IsResolved to true) but this method retrieves it nevertheless.

Edit2:

Properties after editing data manually in database

Data in the database

Edit3:

var requests = AccessContext.Requests.Where(o => o.Date >= fromDate && o.Date <= toDate).ToList();
        foreach (var request in requests) {
            AccessContext.Entry(request).Reload();
        }
        return requests;

Final Question: The code above "solves" the problem - but in my opinion it's not clean. Is there another way?

like image 656
Th1sD0t Avatar asked Nov 08 '22 10:11

Th1sD0t


1 Answers

When you access an entity on a database, the entity is cached (and tracked to track changes that your application does until you specify AsNoTracking).
This has some issues (for example, performance issues because the cache increases or you see an old version of entities that is your case).
For this reasons, when using EF you should work with Unit of work pattern (i.e. you should create a new context for every unit of work).

You can have a look to this Microsoft article to understand how implement Unit of work pattern. http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

In your case using Reload is not a good choice because the application is not scalable. For every reload you are doing a query to database. If you just need to return desired entities the best way is to create a new context.

public IEnumerable<Request> GetAllUnResolvedRequests() 
{
    return GetNewContext().Requests.Where(o => !o.IsResolved).ToList();
}
like image 154
bubi Avatar answered Nov 22 '22 11:11

bubi