AppFabric fails with EF4 (Entity Framework) Code First Model because of DynamicProxies

Saran Jawaban AppFabric fails with EF4 (Entity Framework) Code First Model because of DynamicProxies

  • Thursday, October 21, 2010 3:06 AM
     
     

    I am using the releast version of AppFabric (as of 20 Oct 2010) against an Entity Framework 4 Code-First data model. As part of this, we are drawing items from SQL into POCO objects and storing those objects in AppFabric.  We can store/retrieve items with no problems.

    When I recompile the code and try to retrieve an item (which I know is still in the cache), I get the following exception:

    "The deserializer cannot load the type to deserialize because type 'System.Data.Entity.DynamicProxies.ACMEVersionHisto_0274F07D5CB3ECB757E0533C2AD895AD967B6E8D00FAB202E813E9C48E9493A5' could not be found in assembly 'EntityFrameworkDynamicProxies-ACME.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Check that the type being serialized has the same contract as the type being deserialized and the same assembly is used."

    I believe that the dynamic proxy classes used by EF4 to support the POCO model are recompiled with different signatures which are not backwards compatible with those items still stored in the cache - even though the structure of the class remains unchanged (ie same properties/fields/etc).  Makes sense I guess, but it makes these latest two MS technologies completely incompatible, which I find hard to believe.

    Of note, we used to use the HttpRuntime.Cache for our caching requirements, and never had any problems.

    Any help, much appreciated - thanks.

     

All Replies

  • Wednesday, May 11, 2011 2:35 PM
     
     Proposed Answer

     

    I found a solution to this by disabling proxy creation using the ProxyCreationEnabled property on the db context configuration object.  I am using EF 4.1.

    This does however disable change tracking and removes support for lazy loading, neither of which I needed.

    http://msdn.microsoft.com/en-us/library/dd456853.aspx

    -Thanks.

    Brian



    • Proposed As Answer by TheNothingness Wednesday, May 11, 2011 2:56 PM
    •  
  • Wednesday, May 11, 2011 10:09 PM
     
     
    I also recall we simpily removed all the virtual properties from our POCO objects.
  • Tuesday, March 20, 2012 10:17 AM
     
     

    Hi,

    I too facing the same like issue. I am using EF 4.1 and inmplementing DbContext class, and creating the object collection by overriding the OnModelCreating(). This works for most of the entities but fails with one having large no of records in DB table.

    I am able to get data from DB and managed to put the entity objects in AppServerFabric cache, but getting the mentioned error when I read them back from Cache.

    I don't know where to put the code for suggested context.ContextOptions.ProxyCreationEnabled = false;

    Can you please guide me?

    thanks

    Vipin

  • Tuesday, March 20, 2012 7:57 PM
     
     
    Hi - my answer remains the same - I just took all virtual properties off my POCO objects, so EF simply can't create dynamic proxies.
  • Wednesday, March 21, 2012 9:05 AM
     
     

    I don't want to remove Virtual properties, as they are there to load dependent objects.

    If I read the data from DB, creates objects using EF, Load them into AppFabricCache, Read again from Cache in a single step it works fine.

    But if another process tries to read objects from existing AppFabric cache, the mentioned error comes in.

    Should I implement it an any other ways? Suggetions are welcome.

    Thanks

    Vipin

  • Wednesday, March 21, 2012 9:07 AM
     
     

    Below given is the error message.

    Could not load file or assembly 'EntityFrameworkDynamicProxies-CounterPartyRisk.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

  • Thursday, April 05, 2012 10:35 PM
     
     Proposed Answer

    Vipin,

    To ensure that the Proxy Creation is always disabled you may want to put it into the constructor in your class that derives from DbContext. If you used the ADO.NET DbContext generator template to auto-generate the POCO classes and the DbContext classes you can modify the Context.tt that it generated so it will generate the context.ContextOptions.ProxyCreationEnabled = false; in the constuctor. Since Lazy Loading will not work without Dynamic Proxies you might as well turn them both off together. Find the WriteLazyLoadingEnabled method in the context.tt; below this.Configuration.LazyLoadingEnabled = false; add the line this.Configuration.ProxyCreationEnabled = false;

    One other thing: in the EDMX designer set the Lazy Loading Enabled property to false as the above code checks that value.

    • Proposed As Answer by VIPG Tuesday, April 17, 2012 11:54 AM
    •  
  • Tuesday, April 17, 2012 11:54 AM
     
     

    Thank you very much Dottie. The resolution you have provided works in my scenerio without removing the Virtual properties.

    Cheers!!

    Vipin