21 octombrie 2010 03:06
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=126.96.36.199, 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.
11 mai 2011 14:35
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.
- Propus ca răspuns de TheNothingness 11 mai 2011 14:56
11 mai 2011 22:09I also recall we simpily removed all the virtual properties from our POCO objects.
20 martie 2012 10:17
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?
20 martie 2012 19:57Hi - my answer remains the same - I just took all virtual properties off my POCO objects, so EF simply can't create dynamic proxies.
21 martie 2012 09:05
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.
21 martie 2012 09:07
Below given is the error message.
Could not load file or assembly 'EntityFrameworkDynamicProxies-CounterPartyRisk.Entities, Version=188.8.131.52, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
5 aprilie 2012 22:35
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.
- Propus ca răspuns de VIPG 17 aprilie 2012 11:54
17 aprilie 2012 11:54
Thank you very much Dottie. The resolution you have provided works in my scenerio without removing the Virtual properties.