Created
January 30, 2013 22:30
-
-
Save sharpjs/67fb4374f67dbc935cce to your computer and use it in GitHub Desktop.
Make Entity Framework save the dynamic proxy assembly it uses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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