Skip to content

Instantly share code, notes, and snippets.

@sharpjs
Created January 30, 2013 22:30
Show Gist options
  • Save sharpjs/67fb4374f67dbc935cce to your computer and use it in GitHub Desktop.
Save sharpjs/67fb4374f67dbc935cce to your computer and use it in GitHub Desktop.
Make Entity Framework save the dynamic proxy assembly it uses
private void SaveEntityFrameworkProxyAssembly(params Type[] types)
{
var factoryType = typeof(ObjectContext).Assembly.GetType(
"System.Data.Objects.Internal.EntityProxyFactory",
throwOnError: true);
var privateStaticFlags
= BindingFlags.Static
| BindingFlags.NonPublic;
factoryType
.GetField("s_ProxyAssemblyBuilderAccess", privateStaticFlags)
.SetValue(null, AssemblyBuilderAccess.RunAndSave);
(new MyAppsDbContext() as IObjectContextAdapter)
.ObjectContext
.CreateProxyTypes(types);
var moduleBuilders = (IDictionary<Assembly, ModuleBuilder>)
factoryType
.GetField("s_ModuleBuilders", privateStaticFlags)
.GetValue(null);
var proxyAssembly = (AssemblyBuilder)
moduleBuilders[typeof(UserProfile).Assembly].Assembly;
proxyAssembly.Save("EntityProxyModule.dll");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment