This project is read-only.

razor throw an error while the model is a dynamic-generated object from entityframework


I'm using entity framework 4.0 (POCO) and razor engine together, when I write the razor template content as like <a href="@Model.Url" />, this template parses and run correctly when the Model is a common class object, but throws an error while the Model was auto-generated by entityframework. The exception details is:
The type or namespace name 'DynamicProxies' does not exist in the namespace 'System.Data.Entity' (are you missing an assembly reference?)
I'm not sure if this is a bug or not, otherwise what should I do with this case?
Thanks a lot!


cairabbit wrote Mar 29, 2011 at 8:34 AM

in additional, I found that the assemblies generated by entity framework 4, which contains dynamic-generated objects, were loaded to AppDomain, but the razor engine didn't probe the assemblies while resolving the types. Are there any ways to load the custom assemblies to razor engine? for example, it could be Razor.LoadAssembly(...) or Razor.DynamicDirectory=...

BuildStarted wrote Mar 29, 2011 at 3:13 PM

I'm under the impression that this error is related to the same error as the nUnit issues we had (have?) with it not referencing an assembly that wasn't loaded in memory at the time. Try forcing a reference in your code to a class or object within DynamicProxies library. Maybe have a class implement DynamicProxies.IProxy - this may or may not work and unfortunately Antaris knows more about this issue than I do. I'm sure he'll chime in eventually.

BuildStarted wrote Mar 29, 2011 at 3:14 PM

Oh, you can also add assemblies using the config section referenced <a href=''>here>

AntarisZX wrote Mar 29, 2011 at 7:43 PM


I've yet to start playing round properly with EF4 yet, but is the generated assembly at runtime (dynamic) assembly, or a known-compiled assembly with code generated by the designer? When we are loading assemblies referenced in the current AppDomain, we filter out any dynamically generated assemblies.

cairabbit wrote Mar 30, 2011 at 2:45 AM

thanks for reply.
the generated assembly was generated at runtime, it is a in-memory assembly, the generated assembly was named like 'EntityFrameworkDynamicProxies-xxxxx.yyyy...'. I can get the generated assembly on the event of AppDomain.CurrentDomain.AssemblyLoad on the Application_Start in global.asax. So if the razor engine support to add a reference here, that would be fine. However, I think that would be better if the razor engine could auto-reference all the assemblies loaded in the AppDomain.

for BuildStarted: thanks, I've tried that way, the problem is that the generated assembly is not loaded. and the reference namespace is unknown. When debuging, I can see the generated object type would be "System.Data.Entity.DynamicProxies.WebSite-xxxxxxxxxxxxxxx", the WebSite is my known class.

wrote Feb 22, 2013 at 1:17 AM

wrote Feb 11, 2014 at 2:39 PM

wrote May 26, 2014 at 9:56 AM