none
How to cache with entity framework RRS feed

  • Question

  • I know this question is asked before, but those threads are old and do not provide a satisfactory answer.

    I have a few tables that need to be cached as static read only objects. I want to use System.Runtime.Caching, since I read that that is the answer to my problems... However, it now gives me uncontrollable problems (errors stating it can't find related objects or it can't find functions like "get_tbStates"). When I add an entity to the cache:

    myCache.Countries = context.tbCountries.ToList();
    foreach (tbCountries item in myCache.Countries)
    {
       if (!item.tbStates.IsLoaded)
       item.tbStates.Load();
    }

    the MemoryCache also stores the context which sometimes closes or becomes invalid or whatnot. I don't need the context, I want just the List<tbCountries> of Countries. And every Country object must still have it's List<tbStates> of States when I get it from the Cache.

    I tried the following strategies:

    context.ContextOptions.LazyLoadingEnabled = false;
    context.ContextOptions.ProxyCreationEnabled = false;
    context.tbCountries.MergeOption = MergeOption.NoTracking;

    But I'm not sure the error will not emerge again at some unpredictable time...

    I also tried to use: context.Detach(some_Country); But the moment I detach the country, all states are lost.

    I used Shared Cache which serializes the objects to xml (thereby effectively removing all unneeded baggage), but it was too slow. I think I'm now going to use shared cache again, but load the "shared cache" cached objects into the MemoryCache just for speed. But it does not seem right... why use two caching mechanisms? There must be a way to just store the plain old entity objects as detached objects, with their related entities, without anything else... There must be other developers that want to cache readonly entity framework entities???

    So my question is: What is the best way to store detached entity framework objects with System.Runtime.Caching? 

    • Moved by VMazurModerator Tuesday, June 14, 2011 10:37 AM (From:ADO.NET Managed Providers)
    Friday, June 10, 2011 11:05 AM

Answers

All replies

  • Hi roog,

    Welcome!

    I think you can use Cache provider here: http://blogs.msdn.com/b/adonet/archive/2010/09/13/ef-caching-with-jarek-kowalski-s-provider.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 13, 2011 2:56 AM
    Moderator
  • Hi roog,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 17, 2011 7:55 AM
    Moderator
  • Hi Alan,

    First of all thanks for the response, I'm still looking into your suggestion to use Jarek Kowalski's cache. I did not mark it as an answer because it does not do exactly what I want: to "extract" all entities plus relations as pure objects (without context or any other relation to entity framework), and without mapping them to separate entities, into a caching mechanism. Kowalski's solution provides a nice way to store the entities in cache, but it keeps the possibility to re-attach them to the context. I accept your answer as being "marked as answer" because I don't think there will be a better answer. It might even be interesting to actually keep a connection to entity framework for the possible occasion that I do need to re-attach cached objects (like countries, states and all those inert  things).

    Anyway, my solution so far is to serialize my cache objects into SharedCache, thereby loosing all connection to the entity framework. I can then deserialize those objects into any other application cache (or session) to keep it fast. 

    thanks,

     

     



    Thursday, June 30, 2011 7:25 AM