Reputation: 1333
I'm using EF 4.3.1 and I have 100+ contexts which drives 1 base context. For all contexts I want to disable Database Initialization.
Is it possible to set default behavior for Entity Framework?
Thanks in advance
Upvotes: 3
Views: 5204
Reputation: 843
//Gather all the DbContexts types in the assembly
var allDbContextsTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.BaseType == (typeof(DbContext))).ToList();
foreach (Type dbContextType in allDbContextsTypes)
{
//Get the SetInitializerMethod
MethodInfo initializerMethod = typeof(Database).GetMethod("SetInitializer");
//Make it generic! (Oh yeah)
MethodInfo dbContextInitializerMethod = initializerMethod.MakeGenericMethod(dbContextType);
//Invoke the method with null initializer
dbContextInitializerMethod.Invoke(null, new object[]{null});
}
In the end, it gives something like : Database.SetInitializer<YourDbContext>(null);
where YourDbContext is the current dbContext type in your loop
Upvotes: 2
Reputation: 1338
You can change your *.config file to disable database initialization. This is done per context, but maybe it will work for your base context.
<contexts>
<context type=" Blogging.BlogContext, MyAssembly" disableDatabaseInitialization="true" />
</contexts>
A much better explanation is given in EF 4.3 Configuration File Settings
Upvotes: 2
Reputation: 3243
Database.SetInitializer<TContext>
is what you are after. Also passing null to this method should disable initialization.
http://msdn.microsoft.com/en-us/library/gg679461(v=vs.103).aspx
Upvotes: 2