Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
public static void ProvisionTenant(string tenantSchema, DbConnection connection)
{
using(DbContext ctx = Create(tenantSchema, connection))
{
if (!ctx.Database.Exists())
{
ctx.Database.Create();
}
else
{
// Clear EF memory
Database.SetInitializer(null);
// Retrieve ObjectContext of DbContext in order to retrieve the DB Scripts
IObjectContextAdapter adapter = ((IObjectContextAdapter) ctx);
ObjectContext objectContext = adapter.ObjectContext;
string createScript = objectContext.CreateDatabaseScript();
// Once the script is retrieved in memory, generate it for the new schema
ctx.Database.ExecuteSqlCommand(createScript);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment