none
Working with detached objects (in-memory context)

    Question

  •    I work  with EF and I like It. But in real situations it works very slowly, especially when big amount of objects are handled. I think that It will be good to have ability to work with EF-objects in memory. The best solution, I guess, is to have some special context (lets call it Iin-memory context'), which should support a lot of EF features (e.g. navigation properties). And work with this context should be organized somehow like this: I query objects from EF context, detach all the entities and put them in the in-memory context. After that I can query/add/delete/modify objects and relationships in memory and after that I can attach all objects (old and new) in the EF-context and save it to the database.
       The questions:
    1. I haven't found any solutions linke 'in-memory' context. Do they exist?
    2. I want to try to implement something like in-memory context. Is it posiible? I mean,don't exist some limitations concerning detached entities, which can't allow me to do it? For example - can I efficiently use navigation properties in detached objects? Of cource, I understand tha in-memory context should do a lot of works with objects. Do some pitfalls exist?
    Tuesday, December 2, 2008 1:58 PM

All replies

  • I have some question for you.

     

    - If you want to emprove performance why do you put object in memory? it is so slow and require a lot of memory.

    If you try to do a ado.net you give best performance. read this post http://forums.microsoft.com/Forums/ShowPost.aspx?PostID=4151490&SiteID=1 to generate sql from linq and get pure sql to use over ado.net.

     

    - In order to reduce the number of accass to DB use eager loading http://blogs.msdn.com/adonet/archive/2008/10/07/migrating-from-linq-to-sql-to-entity-framework-eager-loading.aspx

     

    - do you need view? http://msdn.microsoft.com/en-us/library/bb896240.aspx

     

    - it can help you? http://msmvps.com/blogs/matthieu/archive/2008/05/31/entity-cloner.aspx

     

    - use MergeOption.NoTracking in order to perform a context detached. read here http://www.scip.be/index.php?Page=ArticlesNET16

     

    that's all.

    I hope it is helpfull for you

    Tuesday, December 2, 2008 3:46 PM
  •  

    I can add few points if you do not know. if you using entities for readonly scenarios u should consider calling Execute with MergeOption.NOTracking. It is very light weight, it does not bring additional relations and does not have any foot print cuz the objects are not tracked inside the state manager. Secondly results are returned without identity resolution so you do njot incur the overhead for identity checking. In addition if later u decide that you want to insert, update, you can attach the entity to the context, and then start making changes.

     

    Zeeshan Hirani

    Tuesday, December 2, 2008 3:51 PM
  •    The reason of slow performance is only in big amount of queries to the database. I have a lot of independent classes which query object context. And it happens permanently (in the Idle handler). And just because of that, there are nearly 10 000 queries per minute to the database. But there is not necessary to query some collections of objects 10 times per second, because this coellction changes rarely. And just because of that I need some place in memory where I can store all my obejcts. Firstly I decided to implenet some cache. And it really works not bad. I mannualy reset this cache when it is necessary. But I don't like this approach, because there are exist queries to the database. There are not many queries, but they are. And it is also not very convenient. I have to mannualy reset the cache.
       The best way to work with objects in my situation, I think, is to work with some in-memory context. Using this context I can query any objects without queries to the database (it is necessary to perform only first query to database to retrieve). But now the problem is detached objects (with NoTraking merge option) are returned without relationships. Even when I receive attached object whith correct association link, this link is deleted when I detach object from the context. I need relationships to be correctly set when object have being loaded.
        How can I do it?
       The only way which I have found is to load all the objects from context, detach all the objects (all relationships (links to other objects) is deleted), and restore this relationalship using the entityKey of reference-proprties. By the way, do you know why, when I detach property, the EntityKey property of EntityReference<T> is initialized, and when I load objects from context with NoTracking merge option, this property is not initialized?

     How can I get detached objects with relationships? I mean, that one detached object should have a link to detached entity (or set of detached entities).
       The information in given links are interesting. Thank you. But it can't help me in my situations.
    Tuesday, December 2, 2008 6:11 PM