public static void DeleteLegacyTable() { if (_db.TableExist(_dbName)) { // delete the db table _db.DropTable <AcProductDto>(); } }
private void CreateTable(bool overwrite, Type modelType, SqlCeSyntaxProvider syntaxProvider) { var defFactoryType = Type.GetType("Umbraco.Core.Persistence.DatabaseModelDefinitions.DefinitionFactory,Umbraco.Core", true); var tableDefinition = (TableDefinition)defFactoryType.CallStaticMethod("GetTableDefinition", modelType); var tableName = tableDefinition.Name; string createSql = syntaxProvider.Format(tableDefinition); string createPrimaryKeySql = syntaxProvider.FormatPrimaryKey(tableDefinition); var foreignSql = syntaxProvider.Format(tableDefinition.ForeignKeys); var indexSql = syntaxProvider.Format(tableDefinition.Indexes); var tableExist = _db.TableExist(tableName); if (overwrite && tableExist) { _db.DropTable(tableName); tableExist = false; } if (tableExist == false) { using (var transaction = _db.GetTransaction()) { //Execute the Create Table sql int created = _db.Execute(new Sql(createSql)); LogHelper.Info <ExternalLoginStore>(string.Format("Create Table sql {0}:\n {1}", created, createSql)); //If any statements exists for the primary key execute them here if (!string.IsNullOrEmpty(createPrimaryKeySql)) { int createdPk = _db.Execute(new Sql(createPrimaryKeySql)); LogHelper.Info <ExternalLoginStore>(string.Format("Primary Key sql {0}:\n {1}", createdPk, createPrimaryKeySql)); } //Turn on identity insert if db provider is not mysql if (syntaxProvider.SupportsIdentityInsert() && tableDefinition.Columns.Any(x => x.IsIdentity)) { _db.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} ON ", syntaxProvider.GetQuotedTableName(tableName)))); } //Turn off identity insert if db provider is not mysql if (syntaxProvider.SupportsIdentityInsert() && tableDefinition.Columns.Any(x => x.IsIdentity)) { _db.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} OFF;", syntaxProvider.GetQuotedTableName(tableName)))); } //Loop through foreignkey statements and execute sql foreach (var sql in foreignSql) { int createdFk = _db.Execute(new Sql(sql)); LogHelper.Info <ExternalLoginStore>(string.Format("Create Foreign Key sql {0}:\n {1}", createdFk, sql)); } //Loop through index statements and execute sql foreach (var sql in indexSql) { int createdIndex = _db.Execute(new Sql(sql)); LogHelper.Info <ExternalLoginStore>(string.Format("Create Index sql {0}:\n {1}", createdIndex, sql)); } transaction.Complete(); } } LogHelper.Info <ExternalLoginStore>(string.Format("New table '{0}' was created", tableName)); }