public static void DeleteDatabaseIfExists() { using (DatabaseContext _DatabaseContext = new DatabaseContext()) { if (_DatabaseContext.DatabaseExists()) _DatabaseContext.DeleteDatabase(); } }
public static void CreateDatabaseIfDoesNotExist() { using (DatabaseContext _DatabaseContext = new DatabaseContext()) { if (!_DatabaseContext.DatabaseExists()) _DatabaseContext.CreateDatabase(); } }
protected void buttonCreateDatabase_Click(object sender, EventArgs e) { try { string newConnectionString = BuildEntityFrameworkConnectionStringForSqlClient ( txtServerAddress.Text, txtDbName.Text, chkUseIntegratedSecurity.Checked, txtUsername.Text, txtPassword.Text ); //create database using (DatabaseContext _DatabaseContext = new DatabaseContext()) { ((EntityConnection)_DatabaseContext.Connection).StoreConnection.ConnectionString = BuildSqlClientConnectionString ( txtServerAddress.Text, txtDbName.Text, chkUseIntegratedSecurity.Checked, txtUsername.Text, txtPassword.Text ); if (!_DatabaseContext.DatabaseExists()) { _DatabaseContext.CreateDatabase(); } } //save the new db connectionstring to config files SaveConnectionStringKey(Server.MapPath("~/web.config"), "DatabaseContext", newConnectionString); //show the proper message labelMessage.Text = MessageFormatter.GetFormattedNoticeMessage("Database created successfully. Click the Create 'Install Schema and Data' button to install database."); buttonInstall.Visible = true; txtDbName.ReadOnly = true; } catch (Exception ex) { labelMessage.Text = MessageFormatter.GetFormattedErrorMessage("Database creation failed: " + ex.Message); buttonInstall.Visible = false; } }