public void Load_reference_dbType_should_be_valid() { string testMapfile = Path.Combine(SessionHelper.BaseDirectory, "TestFiles", "MapConfigTests", "TestFullMap.xml"); MapConfig config = new MapConfig(); config.Load(testMapfile); var ent = config.GetEntityMap("WebZoo.Data.Animal"); var rel = ent.Relations["Zoo"]; Assert.AreEqual(System.Data.DbType.Int32, rel.DbType); }
void ImportData(MapConfig mapConfig, SqlConnection conn, SqlTransaction transaction, AppOptionInfo opts, string filePath) { var exportedData = ExportedDataModel.LoadFromFile(filePath); var ent = mapConfig.GetEntityMap(exportedData.EntityName); Logger.Log(LogLevel.Debug, $"[{ent.FullName}] - table: {ent.TableName} - rows: {exportedData.DataRows.Count}"); string destinationTableName = $"[{ent.SchemaName}].[{ent.TableName}]"; try { DataTable table = ent.CreateTable(); RazorInterpreter interpreter = new RazorInterpreter(); int count = 0; if (opts.Merge) { //create table var tableCreate = interpreter.CompileTemplate(Templates.CreateTempTable, ent); destinationTableName = $"[{ent.SchemaName}].[#{ent.TableName}]"; ExecuteCommand(tableCreate, conn, transaction); } using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, transaction)) { foreach (DataColumn tableColumn in table.Columns) { sqlBulkCopy.ColumnMappings.Add(tableColumn.ColumnName, tableColumn.ColumnName); } foreach (var exportedDataDataRow in exportedData.DataRows) { var dataRow = table.NewRow(); foreach (var field in exportedDataDataRow) { var prop = ent.FindPropertyByColumnName(field.Key); var clrType = SqlTypeHelper.GetClrType(prop.DbType, prop.IsNullable); var column = table.Columns[field.Key]; if (field.Value == null || string.IsNullOrWhiteSpace(field.Value.ToString())) { dataRow[field.Key] = DBNull.Value; } else { var convertMethod = converter.GetConverterFactoryMethod(clrType); var value = convertMethod(field.Value.ToString().Trim()); dataRow[field.Key] = value ?? DBNull.Value; } } count++; Logger.Log(LogLevel.Debug, "Adding row " + count); table.Rows.Add(dataRow); dataRow.AcceptChanges(); } sqlBulkCopy.DestinationTableName = destinationTableName; Logger.Log(LogLevel.Debug, $"Data table [{sqlBulkCopy.DestinationTableName}] loaded. Will now write to the database."); sqlBulkCopy.WriteToServer(table); if (opts.Merge) { var mergeScript = interpreter.CompileTemplate(Templates.Merge, ent); ExecuteCommand(mergeScript, conn, transaction); ExecuteCommand($"DROP TABLE {destinationTableName}", conn, transaction); } table.Clear(); } } catch (Exception ex) { Logger.Log(LogLevel.Error, $"Error trying to import data to {ent.TableName}..."); throw; } }