Created
November 12, 2014 00:02
-
-
Save sjwaight/3c2209dfc3ab6e463a04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ContosoCustomDatabaseInitializer : IDatabaseInitializer<SchoolContext> | |
{ | |
public void InitializeDatabase(SchoolContext context) | |
{ | |
if (context.Database.Exists()) | |
{ | |
if (!context.Database.CompatibleWithModel(true)) | |
{ | |
context.Database.Delete(); | |
} | |
} | |
CreateAzureSqlDatbase(); | |
// could check that the DB exists | |
// could execute SQL on the server now if it does | |
} | |
private void CreateAzureSqlDatbase() | |
{ | |
// could parse the connection string and extract these values if we wanted to. | |
var serverName = CloudConfigurationManager.GetSetting("AzureSqlDatabaseServerName"); | |
var databaseName = CloudConfigurationManager.GetSetting("AzureSqlDatabaseName"); | |
var subscriptionId = CloudConfigurationManager.GetSetting("AzureSubscriptionId"); | |
var certificateThumprint = CloudConfigurationManager.GetSetting("AzureManagementCertThumprint"); | |
// would need to upload your management certificate along with your web application | |
var certificate = LoadCertificate(certificateThumprint); | |
var credentials = new CertificateCloudCredentials(subscriptionId, certificate); | |
using (var client = new SqlManagementClient(credentials)) | |
{ | |
var database = client.Databases.Get(serverName, databaseName).Database; | |
if (database == null) | |
{ | |
var creationParameters = new DatabaseCreateParameters | |
{ | |
Name = databaseName, | |
Edition = "Standard", | |
MaximumDatabaseSizeInGB = 10, | |
}; | |
client.Databases.Create(serverName, creationParameters); | |
} | |
} | |
} | |
private X509Certificate2 LoadCertificate(string certThumprint) | |
{ | |
// load the certificate from the appropriate location. | |
// There's a bunch of samples online on how to do this. | |
// See code sample here for how: http://msdn.microsoft.com/en-us/library/azure/dn505701.aspx | |
return new X509Certificate2(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment