/// <summary> /// Add manual mapping function for specifies table /// </summary> /// <param name="table">Table name</param> /// <param name="manualMapping">Manual mapping function for table</param> public void AddManualMapping(string table, ManualMapping manualMapping) { if (this._configManualMapping.ContainsKey(table) == false) { this._configManualMapping.Add(table, manualMapping); } }
/// <summary> /// Start manual mapping function /// </summary> /// <param name="tableName"></param> private void MapManual(string tableName) { // // Get manual mapping for table ManualMapping manualMapping = this._configManualMapping[tableName]; // // If this table override all mapping process // Then invoke Map function only if (manualMapping.ManualMappingAllProcess) { try { manualMapping.Map(this, this._sourceDatabase, this._destinationDatabase); this._destinationDatabase.SaveChanges(); } catch (Exception excManualMappingAllProcess) { LogService.Log.Info("Error when mapping with " + tableName, excManualMappingAllProcess); } } else { manualMapping.BeforeMapping(this, this._sourceDatabase, this._destinationDatabase); // // Get mapping config // Which talble in source database will map with this table ? // By default, two table have the same name will be mapped string sourceTableName = this.GetTableOldName(tableName); // // Initialize for auto mapping DbSet sourceTable = this._sourceDatabase.GetTable(sourceTableName); DbSet destTable = this._destinationDatabase.GetTable(tableName); if (sourceTable == null) { LogService.Log.Error("Can not get " + sourceTableName + " DbSet from source database."); return; } if (destTable == null) { LogService.Log.Error("Can not get " + tableName + " DbSet from destination database."); return; } // // Load and count LogService.Log.Info("Loading table into memory."); sourceTable.Load(); IList listRecords = sourceTable.Local; int totalRecord = listRecords.Count; LogService.Log.Info("Number of records : " + totalRecord.ToString()); // // First, try to run automapper with multi threading LogService.Log.Info("Run AutoMapper with multi threading."); List <object> listNewRecord = this._multiThreadManager.RunAutoMapperMultiThread(listRecords, totalRecord, sourceTableName, tableName, sourceTable.ElementType, destTable.ElementType, manualMapping).ToList(); // // Second, add new record to database, then save changes manualMapping.BeforeCommitChanges(this, this._sourceDatabase, this._destinationDatabase, listNewRecord.ToList()); LogService.Log.Info("Inserting records to destination database."); if (this.IsGeneratedScript) { string filePath = this.GetFilePath(tableName); LogService.Log.Info("Generate migration script to file, file path : " + filePath); this._sqlMultiThreadManager.Insert(listNewRecord, tableName, this._destinationDatabase.GetTableTypeAccessor(tableName), this._destinationDatabase.GetTableColumn(tableName), filePath); } else { this._sqlMultiThreadManager.Insert(listNewRecord, tableName, this._destinationDatabase.GetTableTypeAccessor(tableName), this._destinationDatabase.GetTableColumn(tableName)); } manualMapping.AfterCommitChanges(this, this._sourceDatabase, this._destinationDatabase); // // Done manualMapping.AfterMapping(this, this._sourceDatabase, this._destinationDatabase); LogService.Log.Info("Mapping " + tableName + " done."); } }