var allDbContextsTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.BaseType == (typeof(DbContext))).ToList();
foreach (Type dbContextType in allDbContextsTypes)
{
MethodInfo initializerMethod = typeof(Database).GetMethod("SetInitializer");
MethodInfo dbContextInitializerMethod = initializerMethod.MakeGenericMethod(dbContextType);
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
source
share