Serializing Entity Framework objects for Azure Cache


  • We use Azure Caching directly (and not through one of the available Entity Framework wrappers). Apparently, for distributed caching, we need to serialize the objects. Unfortunately, this causes issues with lazy-loaded DbContext-based proxies used for navigation properties.

    I see we can use a custom serializer in order to map proxies to empty collections (if not loaded) or to normal objects (if loaded), but I am not sure about the implementation. One possible implementation can be based on the one used by WCF, but I am not sure Azure works the same way.

    Has anyone manually fixed that problem in an elegant way?

    Thanks in advance!

    Goblin Dice Roller for your RPG games!

    Friday, January 11, 2013 4:59 PM


  • Thanks but I try to find something more elegant. A suggestion in StackOverflow is simply to detach the object before putting it into the cache and then attach it to the new context when you get it back. We're still trying to find something better.

    Streamlined simplicity

    Monday, January 14, 2013 3:55 PM
  • Unfortunately this is not the proper answer. Detaching an object will not detach objects on its navigation properties. Identifying which objects are loaded, thus which should be detached, for a particular operation, is extremely complicated.
    The only other workaround (presented in StackOverflow) would be to disable DbContext.Configuration.ProxyCreationEnabled before every query loading an object needed for the cache - but this would force us to remove lazy loading from a various places in our application.

    Streamlined simplicity

    Friday, January 18, 2013 9:50 AM

All replies