AppFabric fails with EF4 (Entity Framework) Code First Model because of DynamicProxies
-
2010年10月21日 3: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=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.
すべての返信
-
2011年5月11日 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.
http://msdn.microsoft.com/en-us/library/dd456853.aspx
-Thanks.
Brian
- 回答の候補に設定 TheNothingness 2011年5月11日 14:56
-
2011年5月11日 22:09I also recall we simpily removed all the virtual properties from our POCO objects.
-
2012年3月20日 10:17
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
-
2012年3月20日 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.
-
2012年3月21日 9: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.
Thanks
Vipin
-
2012年3月21日 9:07
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.
-
2012年4月5日 22:35
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.
- 回答の候補に設定 VIPG 2012年4月17日 11:54
-
2012年4月17日 11:54
Thank you very much Dottie. The resolution you have provided works in my scenerio without removing the Virtual properties.
Cheers!!
Vipin

