diff --git a/src/Dynamicweb.DataIntegration.Providers.SqlProvider.csproj b/src/Dynamicweb.DataIntegration.Providers.SqlProvider.csproj index 3369803..988b8bc 100644 --- a/src/Dynamicweb.DataIntegration.Providers.SqlProvider.csproj +++ b/src/Dynamicweb.DataIntegration.Providers.SqlProvider.csproj @@ -1,6 +1,6 @@  - 10.6.0 + 10.7.0 1.0.0.0 SQL Provider SQL Provider @@ -23,7 +23,7 @@ snupkg - + diff --git a/src/SQLDestinationWriter.cs b/src/SQLDestinationWriter.cs index 176abfb..d83c7df 100644 --- a/src/SQLDestinationWriter.cs +++ b/src/SQLDestinationWriter.cs @@ -91,7 +91,7 @@ public SqlDestinationWriter(Mapping mapping, SqlConnection connection, bool remo public SqlDestinationWriter(Mapping mapping, SqlConnection connection, bool removeMissingAfterImport, ILogger logger, string tempTablePrefix, bool discardDuplicates) { Mapping = mapping; - _columnMappings = Mapping.GetColumnMappings(); + _columnMappings = new ColumnMappingCollection(ReplaceKeyColumnsWithAutoIdIfExists(Mapping)); SqlCommand = connection.CreateCommand(); SqlCommand.CommandTimeout = 1200; this.removeMissingAfterImport = removeMissingAfterImport; @@ -118,7 +118,7 @@ public SqlDestinationWriter(Mapping mapping, SqlConnection connection, bool remo public SqlDestinationWriter(Mapping mapping, SqlConnection connection, bool removeMissingAfterImport, ILogger logger, bool discardDuplicates) { Mapping = mapping; - _columnMappings = Mapping.GetColumnMappings(); + _columnMappings = new ColumnMappingCollection(ReplaceKeyColumnsWithAutoIdIfExists(Mapping)); SqlCommand = connection.CreateCommand(); SqlCommand.CommandTimeout = 1200; this.removeMissingAfterImport = removeMissingAfterImport; @@ -145,7 +145,7 @@ public SqlDestinationWriter(Mapping mapping, SqlConnection connection, bool remo public SqlDestinationWriter(Mapping mapping, SqlCommand mockSqlCommand, bool removeMissingAfterImport, ILogger logger, string tempTablePrefix, bool discardDuplicates) { Mapping = mapping; - _columnMappings = Mapping.GetColumnMappings(); + _columnMappings = new ColumnMappingCollection(ReplaceKeyColumnsWithAutoIdIfExists(Mapping)); SqlCommand = mockSqlCommand; this.removeMissingAfterImport = removeMissingAfterImport; this.logger = logger; @@ -179,6 +179,24 @@ public SqlDestinationWriter(Mapping mapping, SqlCommand mockSqlCommand, bool rem } } + private static IEnumerable ReplaceKeyColumnsWithAutoIdIfExists(Mapping mapping) + { + //will move this to MappingExtensions - US https://dev.azure.com/dynamicwebsoftware/Dynamicweb/_workitems/edit/20900 + if (mapping == null) return []; + + var autoIdDestinationColumnName = MappingExtensions.GetAutoIdColumnName(mapping.DestinationTable?.Name ?? ""); + if (string.IsNullOrEmpty(autoIdDestinationColumnName)) return mapping.GetColumnMappings(); + + var columnMappings = mapping.GetColumnMappings().ToList(); + var autoIdColumnMapping = columnMappings.Where(obj => obj.DestinationColumn.Name.Equals(autoIdDestinationColumnName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); + if (autoIdColumnMapping != null) + { + columnMappings.ForEach(obj => obj.IsKey = false); + autoIdColumnMapping.IsKey = true; + } + return columnMappings; + } + /// /// Writes the specified row. ///