diff --git a/src/Core/Repositories/SqlServer/CipherRepository.cs b/src/Core/Repositories/SqlServer/CipherRepository.cs index 55e2a3163..dfbd5446e 100644 --- a/src/Core/Repositories/SqlServer/CipherRepository.cs +++ b/src/Core/Repositories/SqlServer/CipherRepository.cs @@ -314,7 +314,7 @@ namespace Bit.Core.Repositories.SqlServer using(var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, transaction)) { bulkCopy.DestinationTableName = "#TempCipher"; - var dataTable = BuildCiphersTable(ciphers); + var dataTable = BuildCiphersTable(bulkCopy, ciphers); bulkCopy.WriteToServer(dataTable); } } @@ -324,7 +324,7 @@ namespace Bit.Core.Repositories.SqlServer using(var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, transaction)) { bulkCopy.DestinationTableName = "#TempFolder"; - var dataTable = BuildFoldersTable(folders); + var dataTable = BuildFoldersTable(bulkCopy, folders); bulkCopy.WriteToServer(dataTable); } } @@ -420,7 +420,7 @@ namespace Bit.Core.Repositories.SqlServer using(var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, transaction)) { bulkCopy.DestinationTableName = "#TempCipher"; - var dataTable = BuildCiphersTable(ciphers); + var dataTable = BuildCiphersTable(bulkCopy, ciphers); bulkCopy.WriteToServer(dataTable); } @@ -489,7 +489,7 @@ namespace Bit.Core.Repositories.SqlServer using(var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, transaction)) { bulkCopy.DestinationTableName = "[dbo].[Folder]"; - var dataTable = BuildFoldersTable(folders); + var dataTable = BuildFoldersTable(bulkCopy, folders); bulkCopy.WriteToServer(dataTable); } } @@ -497,7 +497,7 @@ namespace Bit.Core.Repositories.SqlServer using(var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, transaction)) { bulkCopy.DestinationTableName = "[dbo].[Cipher]"; - var dataTable = BuildCiphersTable(ciphers); + var dataTable = BuildCiphersTable(bulkCopy, ciphers); bulkCopy.WriteToServer(dataTable); } @@ -536,7 +536,7 @@ namespace Bit.Core.Repositories.SqlServer using(var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, transaction)) { bulkCopy.DestinationTableName = "[dbo].[Cipher]"; - var dataTable = BuildCiphersTable(ciphers); + var dataTable = BuildCiphersTable(bulkCopy, ciphers); bulkCopy.WriteToServer(dataTable); } @@ -545,7 +545,7 @@ namespace Bit.Core.Repositories.SqlServer using(var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, transaction)) { bulkCopy.DestinationTableName = "[dbo].[Collection]"; - var dataTable = BuildCollectionsTable(collections); + var dataTable = BuildCollectionsTable(bulkCopy, collections); bulkCopy.WriteToServer(dataTable); } @@ -554,7 +554,7 @@ namespace Bit.Core.Repositories.SqlServer using(var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, transaction)) { bulkCopy.DestinationTableName = "[dbo].[CollectionCipher]"; - var dataTable = BuildCollectionCiphersTable(collectionCiphers); + var dataTable = BuildCollectionCiphersTable(bulkCopy, collectionCiphers); bulkCopy.WriteToServer(dataTable); } } @@ -576,7 +576,7 @@ namespace Bit.Core.Repositories.SqlServer } } - private DataTable BuildCiphersTable(IEnumerable ciphers) + private DataTable BuildCiphersTable(SqlBulkCopy bulkCopy, IEnumerable ciphers) { var c = ciphers.FirstOrDefault(); if(c == null) @@ -607,6 +607,11 @@ namespace Bit.Core.Repositories.SqlServer var revisionDateColumn = new DataColumn(nameof(c.RevisionDate), c.RevisionDate.GetType()); ciphersTable.Columns.Add(revisionDateColumn); + foreach(DataColumn col in ciphersTable.Columns) + { + bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName); + } + var keys = new DataColumn[1]; keys[0] = idColumn; ciphersTable.PrimaryKey = keys; @@ -632,7 +637,7 @@ namespace Bit.Core.Repositories.SqlServer return ciphersTable; } - private DataTable BuildFoldersTable(IEnumerable folders) + private DataTable BuildFoldersTable(SqlBulkCopy bulkCopy, IEnumerable folders) { var f = folders.FirstOrDefault(); if(f == null) @@ -653,6 +658,11 @@ namespace Bit.Core.Repositories.SqlServer var revisionDateColumn = new DataColumn(nameof(f.RevisionDate), f.RevisionDate.GetType()); foldersTable.Columns.Add(revisionDateColumn); + foreach(DataColumn col in foldersTable.Columns) + { + bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName); + } + var keys = new DataColumn[1]; keys[0] = idColumn; foldersTable.PrimaryKey = keys; @@ -673,7 +683,7 @@ namespace Bit.Core.Repositories.SqlServer return foldersTable; } - private DataTable BuildCollectionsTable(IEnumerable collections) + private DataTable BuildCollectionsTable(SqlBulkCopy bulkCopy, IEnumerable collections) { var c = collections.FirstOrDefault(); if(c == null) @@ -694,6 +704,11 @@ namespace Bit.Core.Repositories.SqlServer var revisionDateColumn = new DataColumn(nameof(c.RevisionDate), c.RevisionDate.GetType()); collectionsTable.Columns.Add(revisionDateColumn); + foreach(DataColumn col in collectionsTable.Columns) + { + bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName); + } + var keys = new DataColumn[1]; keys[0] = idColumn; collectionsTable.PrimaryKey = keys; @@ -714,7 +729,7 @@ namespace Bit.Core.Repositories.SqlServer return collectionsTable; } - private DataTable BuildCollectionCiphersTable(IEnumerable collectionCiphers) + private DataTable BuildCollectionCiphersTable(SqlBulkCopy bulkCopy, IEnumerable collectionCiphers) { var cc = collectionCiphers.FirstOrDefault(); if(cc == null) @@ -729,6 +744,11 @@ namespace Bit.Core.Repositories.SqlServer var cipherIdColumn = new DataColumn(nameof(cc.CipherId), cc.CipherId.GetType()); collectionCiphersTable.Columns.Add(cipherIdColumn); + foreach(DataColumn col in collectionCiphersTable.Columns) + { + bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName); + } + var keys = new DataColumn[2]; keys[0] = collectionIdColumn; keys[1] = cipherIdColumn; diff --git a/src/Core/Repositories/SqlServer/EventRepository.cs b/src/Core/Repositories/SqlServer/EventRepository.cs index 4481f6f9b..8bb9d8866 100644 --- a/src/Core/Repositories/SqlServer/EventRepository.cs +++ b/src/Core/Repositories/SqlServer/EventRepository.cs @@ -92,7 +92,7 @@ namespace Bit.Core.Repositories.SqlServer using(var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, null)) { bulkCopy.DestinationTableName = "[dbo].[Event]"; - var dataTable = BuildEventsTable(entities.Select(e => e is Event ? e as Event : new Event(e))); + var dataTable = BuildEventsTable(bulkCopy, entities.Select(e => e is Event ? e as Event : new Event(e))); await bulkCopy.WriteToServerAsync(dataTable); } } @@ -131,7 +131,7 @@ namespace Bit.Core.Repositories.SqlServer } } - private DataTable BuildEventsTable(IEnumerable events) + private DataTable BuildEventsTable(SqlBulkCopy bulkCopy, IEnumerable events) { var e = events.FirstOrDefault(); if(e == null) @@ -168,6 +168,11 @@ namespace Bit.Core.Repositories.SqlServer var dateColumn = new DataColumn(nameof(e.Date), e.Date.GetType()); eventsTable.Columns.Add(dateColumn); + foreach(DataColumn col in eventsTable.Columns) + { + bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName); + } + var keys = new DataColumn[1]; keys[0] = idColumn; eventsTable.PrimaryKey = keys;