Note that there are some explanatory texts on larger screens.

plurals
  1. POShort lived DbContext in WPF application reasonable?
    primarykey
    data
    text
    <p>In his book on DbContext, @RowanMiller shows how to use the DbSet.Local property to avoid 1.) unnecessary roundtrips to the database and 2.) passing around collections (created with e.g. ToList()) in the application (page 24). I then tried to follow this approach. However, I noticed that from one using [} – block to another, the DbSet.Local property becomes empty:</p> <pre><code>ObservableCollection&lt;Destination&gt; destinationsList; using (var context = new BAContext()) { var query = from d in context.Destinations …; query.Load(); destinationsList = context.Destinations.Local; //Nonzero here. } //Do stuff with destinationsList using (var context = new BAContext()) { //context.Destinations.Local zero here again; //So no way of getting the in-memory data from the previous using- block here? //Do I have to do another roundtrip to the database here to get the same data I wanted //to cache locally??? } </code></pre> <p>Then, what is the point on page 24? How can I avoid the passing around of my collections if the DbSet.Local is only usable inside the using- block? Furthermore, how can I benefit from the change tracking if I use these short-lived context instances not handing over any cache data to each others under the hood? So, if the contexts should be short-lived for freeing resources such as connections, have I to give up the caching for this? I.e. I can’t use both at the same time (short-lived connections but long-lived cache)? So my only option would be to store the results returned by the query in my own variables, exactly what is discouraged in the motivation on page 24?</p> <p>I am developing a WPF application which maybe will also become multi-tiered in the future, involving WCF. I know Julia has an example of this in her book, but I currently don’t have access to it. I found several others on the web, e.g. <a href="http://msdn.microsoft.com/en-us/magazine/cc700340.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/magazine/cc700340.aspx</a> (old ObjectContext, but good in explaining the inter-layer-collaborations). There, a long-lived context is used (although the disadvantages are mentioned, but no solution to these provided). It’s not only that the single Destinations.Local gets lost, as you surely know all other entities fetched by the query are, too.</p> <p>[Edit]: After some more reading in Julia Lerman’s book, it seems to boil down to that EF does not have 2nd level caching per default; with some (considerable, I think) effort, however, ones can add 3rd party caching solutions, as is also described in the book and in various articles on MSDN, codeproject etc.</p> <p>I would have appreciated if this problem had been mentioned in the section about DbSet.Local in the DbContext book that it is in fact a first level cache which is destroyed when the using {} block ends (just my proposal to make it more transparent to the readers). After first reading I had the impression DbSet.Local would always return the same reference (Singleton-style) also in the second using {} block despite the new DbContext instance.</p> <p>But I am still unsure whether the 2nd level cache is the way to go for my WPF application (as Julia mentions the 2nd level cache in her article for distributed applications)? Or is the way to go to get my aggregate root instances (DDD, Eric Evans) of my domain model into memory by one or some queries in a using {} block, disposing the DbContext and only holding the references to the aggregate instances, this way avoiding a long-lived context? It would be great if you could help me with this decision.</p> <p><a href="http://msdn.microsoft.com/en-us/magazine/hh394143.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/magazine/hh394143.aspx</a> <a href="http://www.codeproject.com/Articles/435142/Entity-Framework-Second-Level-Caching-with-DbConte" rel="nofollow">http://www.codeproject.com/Articles/435142/Entity-Framework-Second-Level-Caching-with-DbConte</a> <a href="http://blog.3d-logic.com/2012/03/31/using-tracing-and-caching-provider-wrappers-with-codefirst/" rel="nofollow">http://blog.3d-logic.com/2012/03/31/using-tracing-and-caching-provider-wrappers-with-codefirst/</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload