Disable database initialization for Entity Framework 4.3

I am using EF 4.3.1 and I have over 100 contexts that control 1 basic context. For all contexts, I want to disable database initialization.

Can I set default behavior for Entity Framework?

Thanks in advance

+3
source share
3 answers
//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});
}

At the end, it gives something like: Database.SetInitializer<YourDbContext>(null); where YourDbContext is the current type of dbContext in your loop

0
source

Database.SetInitializer<TContext>- This is what you need. In addition, passing null to this method should disable initialization.

http://msdn.microsoft.com/en-us/library/gg679461(v=vs.103).aspx

+2
source

You can modify the * .config file to disable database initialization. This is done for each context, but perhaps it will work for your base context.

<contexts>
  <context type=" Blogging.BlogContext, MyAssembly" disableDatabaseInitialization="true" />
</contexts>

A more detailed explanation is given in EF 4.3. Configuration file settings.

+1
source

All Articles