Task. There is a base of films (the film has various fields, in this context it does not matter which ones). In particular, the film has a collection of genres. That is, there is still a table with genres and a table for the connection of the film and the many-to-many genre.

I make a WPF application. The base is ruled by a separate class library. To protect the UI project from EF, I decided to make a separate adapter class ( first question, is it necessary or is it bad practice? ). The adapter is instance. The adapter creates a data context. It turns out that within the scope of the application I have one instance of the adapter (and therefore one instance of the data context). Question two, is this normal? After all, if every time to create a new context, the application will be very long to think. With this approach, I still have a problem with saving data. Well, for example, I opened 2 forms, one for editing information about the film, the other for editing a set of genres. And if I click Save in the form of editing genres (that is, I send the _context.SaveChanges () command), then the changes that I made while editing the film (they were made in the same context) will be entered into the database. Question three, how to work with the data so that this does not happen? And anyway, I can't figure out how to screw it all humanly on MVVM. So far, I am doing govnodic. :( In general, this was the fourth question. How to screw it all on MVVM? In addition, I need a paginal output. So far, only the context.EntitySet.Skip(a).Take(b) method comes to mind. And here is also the question Is this correct? Will such an approach cause a request for all previous values ​​to skip values ​​before returning the necessary set to me?

UPD:

My research led me to what

  1. The adapter is still needed, if only to protect the project UI from working with the Entity Framework.

  2. The context is first created for a long time and makes the first request for a long time (at least the same request to the same network). Each time it uses data from the cache.

  3. Thanks to question 2, question 3 was dropped. But there was a new question. I am making a change to the data in the context instance. Data is saved. I change them from the outside, then I get everything in the same context and it does not pull up third-party changes. That is, it shows the old data. How to tell him to make a new request?

Questions 4 and 5 remained open.

To receive data page by page for MSSQL for me was not that problematic, but not very pleasant (I had to throw a query in the form of text into the procedure), or the query took quite a long time (with large amounts of data). I'm currently working with SQLite. In general, the question of optimization and all that. If someone is sure that EF in this case adequately asks for the page data, please unsubscribe.

Closed due to the fact that the question is too general for the participants Pavel Mayorov , αλεχολυτ , user194374, Vadim Ovchinnikov , Streletz 25 Feb '17 at 18:21 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    First, it seems bad. Skip / Take - it seems he should figure it out himself if you don’t hide him behind your interface. The rest - I do not know. - Qwertiy
  • one
    @Qwertiy, damn it, where are all these people who successfully use EF? I only hear abstract phrases about how useful it is. There are a lot of examples on the Internet, a la ORDER - CUSTOMER, but they are so simple that you should not even consider it. And as I begin to look for specific questions of interest, you will find a fig and no one knows anything :) - iRumba
  • @Qwertiy, and if the first solution is bad, how can I separate the UI from working with EF? I want to operate only with models (Film, Genre) and their collections. For this you do not need EF. Models are ordinary classes. In general, tell us your thoughts on this. - iRumba
  • Consider that it is bll and just wrap on top of linq - it seems so accepted :) - Qwertiy

1 answer 1

To protect the UI project from EF, I decided to make a separate adapter class. You can simply use the class for the data context.

If you create a new context each time, almost nothing will change. Now the power of computing is enough to create a context constantly. You can create a global variable for the context and use it.

With this approach, I still have a problem with saving data. Well, for example, I opened 2 forms, one for editing information about the film, the other for editing a set of genres. And if I click Save in the form of editing genres (that is, I send the _context.SaveChanges () command), then the changes that I made while editing the film (they were made in the same context) will be entered into the database.

The problem with saving should not be. There are 2 open forms and different events for saving information from forms. You need to context.Table.Add (DATA); and context.SaveChanges (); were in the event of clicking the "Save" button.

context.EntitySet.Skip (a) .Take (b) - with such a query a query will be created to the database and it will return only the necessary selection.

  • one
    The events are different, but the context.SaveChanges () will be the same. The context is one. Regarding the context, the problem here is not in the creation, but in the fact that the first query, even from a small table, takes a long time (probably the query is formed for the first time). context.EntitySet.Skip(a).Take(b) returns as much data as needed. The question is, what kind of request will it form? Is it possible that the request will be poorly optimized? - iRumba