private bool HandleDatabaseInitialization()
{
//Must suppress transactions as we can't create a DB in the same
//tx as when we create the schema
using (var supressTranScope = new TransactionScope(TransactionScopeOption.Suppress))
{
if (AnyVersionOfContextHasBeenDeployed())
{
return false;
}
Log.Information(
"Context has yet to be deployed. Running initializer for {contextType} on {endPointServer}\\{endpointDatabase}",
InitializerInfo.ContextType,
m_Config.Database.ServerName,
m_Config.Database.DatabaseName);
using (var targetContext = (DbContext) Activator.CreateInstance(InitializerInfo.ContextType))
{
if (InitializerInfo.Initializer != null)
{
//Have to use reflection here because initializers often
//don't have their TContext generic parmater marked as covariant (in)
typeof (Database).GetMethod("SetInitializer")
.MakeGenericMethod(InitializerInfo.ContextType)
.Invoke(null, new[] { InitializerInfo.Initializer });
}
targetContext.Database.Connection.ConnectionString =
m_ConnectionInfoBuilder.BuildConnectionString(m_Config.Database, m_Config.AuthMode, m_Config.SqlLogin, m_Config.SqlPassword);
targetContext.Database.Initialize(false);
return true;
}
}
}