/// <summary> /// copies the key map table from the dest connection to the source /// so you can do inspect and troubleshoot the mapping in the source connection /// </summary> public async Task <(DbObject @object, int rowCount)> ImportKeyMapTableAsync(DataMigration dataMigration) { DbObject result = null; int rowCount = 0; await ExecuteWithConnectionsAsync(dataMigration, async (source, dest) => { var migrator = await GetMigratorAsync(dest); result = SqlMigrator <int> .KeyMapTable; if (await source.TableExistsAsync(result)) { await source.ExecuteAsync($"DROP TABLE [{result.Schema}].[{result.Name}]"); } if (!await source.SchemaExistsAsync(result.Schema)) { await source.ExecuteAsync($"CREATE SCHEMA [{result.Schema}]"); } string query = $"SELECT * FROM [{result.Schema}].[{result.Name}]"; var data = await dest.QueryTableAsync(query); rowCount = data.Rows.Count; var createTable = await dest.SqlCreateTableAsync(result.Schema, result.Name, query); await source.ExecuteAsync(createTable); await BulkInsert.ExecuteAsync(data, source, result, 35, new BulkInsertOptions() { IdentityInsert = true }); }); return(result, rowCount); }
public static void Initialize(TestContext context) { LocalDb.TryDropDatabase(dbName, out _); using (var cn = LocalDb.GetConnection(dbName, SampleObjects())) { var tdg = new TestDataGenerator(); tdg.Generate <TypicalQueryResult>(1000, (result) => { result.FirstName = tdg.Random(Source.FirstName); result.SomeDate = tdg.RandomInRange(-1000, 1000, (i) => DateTime.Today.AddDays(i)); result.Weight = tdg.RandomInRange <decimal>(50, 150, (d) => d); result.Notes = RandomPhrase(4, 8); }, (results) => { // AppVeyor seems to need this if (cn.State == ConnectionState.Closed) { cn.Open(); } var dataTable = results.ToDataTable(); BulkInsert.ExecuteAsync(dataTable, cn as SqlConnection, DbObject.Parse("dbo.SampleTable"), 50, new BulkInsertOptions() { SkipIdentityColumn = "Id" }).Wait(); }); } }