Skip to content

Commit 91a5d3f

Browse files
authored
Merge pull request #1182 from dendism/master
Fixed bug with keep identity flag in BulkInsert and BulkMerge operations
2 parents f8c2367 + f203aff commit 91a5d3f

File tree

3 files changed

+69
-16
lines changed

3 files changed

+69
-16
lines changed

RepoDb.Extensions/RepoDb.SqlServer.BulkOperations/RepoDb.SqlServer.BulkOperations/Base/BulkInsert.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ private static int BulkInsertInternalBase<TEntity>(SqlConnection connection,
127127
identityDbField?.AsField(),
128128
hints,
129129
dbSetting,
130-
withPseudoExecution);
130+
withPseudoExecution,
131+
options.HasFlag(SqlBulkCopyOptions.KeepIdentity));
131132

132133
// Execute the SQL
133134
using (var reader = (DbDataReader)connection.ExecuteReader(sql, commandTimeout: bulkCopyTimeout, transaction: transaction))
@@ -372,7 +373,8 @@ internal static int BulkInsertInternalBase(SqlConnection connection,
372373
identityDbField?.AsField(),
373374
hints,
374375
dbSetting,
375-
withPseudoExecution);
376+
withPseudoExecution,
377+
options.HasFlag(SqlBulkCopyOptions.KeepIdentity));
376378

377379
// Identify the column
378380
var column = dataTable.Columns[identityDbField.Name];
@@ -532,7 +534,8 @@ private static async Task<int> BulkInsertAsyncInternalBase<TEntity>(SqlConnectio
532534
identityDbField?.AsField(),
533535
hints,
534536
dbSetting,
535-
withPseudoExecution);
537+
withPseudoExecution,
538+
options.HasFlag(SqlBulkCopyOptions.KeepIdentity));
536539

537540
// Execute the SQL
538541
using (var reader = (DbDataReader)(await connection.ExecuteReaderAsync(sql, commandTimeout: bulkCopyTimeout, transaction: transaction, cancellationToken: cancellationToken)))
@@ -782,7 +785,8 @@ internal static async Task<int> BulkInsertAsyncInternalBase(SqlConnection connec
782785
identityDbField?.AsField(),
783786
hints,
784787
dbSetting,
785-
withPseudoExecution);
788+
withPseudoExecution,
789+
options.HasFlag(SqlBulkCopyOptions.KeepIdentity));
786790

787791
// Identify the column
788792
var column = dataTable.Columns[identityDbField.Name];

RepoDb.Extensions/RepoDb.SqlServer.BulkOperations/RepoDb.SqlServer.BulkOperations/Base/BulkMerge.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ private static int BulkMergeInternalBase<TEntity>(SqlConnection connection,
163163
identityDbField?.AsField(),
164164
hints,
165165
dbSetting,
166-
isReturnIdentity.GetValueOrDefault());
166+
isReturnIdentity.GetValueOrDefault(),
167+
options.HasFlag(SqlBulkCopyOptions.KeepIdentity));
167168

168169
// Identity if the identity is to return
169170
if (hasOrderingColumn != true || TypeCache.Get(entityType).IsAnonymousType())
@@ -340,7 +341,8 @@ private static int BulkMergeInternalBase(SqlConnection connection,
340341
identityDbField?.AsField(),
341342
hints,
342343
dbSetting,
343-
false);
344+
false,
345+
options.HasFlag(SqlBulkCopyOptions.KeepIdentity));
344346
result = connection.ExecuteNonQuery(sql, commandTimeout: bulkCopyTimeout, transaction: transaction);
345347

346348
// Drop the table after used
@@ -511,7 +513,8 @@ private static int BulkMergeInternalBase(SqlConnection connection,
511513
identityDbField?.AsField(),
512514
hints,
513515
dbSetting,
514-
isReturnIdentity.GetValueOrDefault());
516+
isReturnIdentity.GetValueOrDefault(),
517+
options.HasFlag(SqlBulkCopyOptions.KeepIdentity));
515518

516519
// Identity if the identity is to return
517520
var column = identityDbField is not null ? dataTable.Columns[identityDbField.Name] : null;
@@ -706,7 +709,8 @@ await WriteToServerAsyncInternal(connection,
706709
identityDbField?.AsField(),
707710
hints,
708711
dbSetting,
709-
isReturnIdentity.GetValueOrDefault());
712+
isReturnIdentity.GetValueOrDefault(),
713+
options.HasFlag(SqlBulkCopyOptions.KeepIdentity));
710714

711715
// Identity if the identity is to return
712716
if (hasOrderingColumn != true || TypeCache.Get(entityType).IsAnonymousType())
@@ -885,7 +889,8 @@ await WriteToServerAsyncInternal(connection,
885889
identityDbField?.AsField(),
886890
hints,
887891
dbSetting,
888-
false);
892+
false,
893+
options.HasFlag(SqlBulkCopyOptions.KeepIdentity));
889894
result = await connection.ExecuteNonQueryAsync(sql, commandTimeout: bulkCopyTimeout, transaction: transaction, cancellationToken: cancellationToken);
890895

891896
// Drop the table after used
@@ -1059,7 +1064,8 @@ await WriteToServerAsyncInternal(connection,
10591064
identityDbField?.AsField(),
10601065
hints,
10611066
dbSetting,
1062-
isReturnIdentity.GetValueOrDefault());
1067+
isReturnIdentity.GetValueOrDefault(),
1068+
options.HasFlag(SqlBulkCopyOptions.KeepIdentity));
10631069

10641070
// Identity if the identity is to return
10651071
var column = identityDbField is not null ? dataTable.Columns[identityDbField.Name] : null;

RepoDb.Extensions/RepoDb.SqlServer.BulkOperations/RepoDb.SqlServer.BulkOperations/SqlConnectionExtension.cs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,8 @@ private static string GetBulkInsertSqlText(string tableName,
613613
Field identityField,
614614
string hints,
615615
IDbSetting dbSetting,
616-
bool isReturnIdentity)
616+
bool isReturnIdentity,
617+
bool keepIdentity)
617618
{
618619
// Validate the presence
619620
if (fields?.Any() != true)
@@ -626,10 +627,21 @@ private static string GetBulkInsertSqlText(string tableName,
626627

627628
// Insertable fields
628629
var insertableFields = fields
629-
.Where(field => string.Equals(field.Name, identityField?.Name, StringComparison.OrdinalIgnoreCase) == false);
630+
.Where(field => keepIdentity == true || string.Equals(field.Name, identityField?.Name, StringComparison.OrdinalIgnoreCase) == false);
630631

631632
// Compose the statement
632-
builder.Clear()
633+
builder.Clear();
634+
635+
// SET IDENTITY_INSERT ON
636+
if (keepIdentity)
637+
{
638+
builder
639+
.WriteText("SET IDENTITY_INSERT")
640+
.TableNameFrom(tableName, dbSetting)
641+
.WriteText("ON;");
642+
}
643+
644+
builder
633645
// MERGE T USING S
634646
.Merge()
635647
.TableNameFrom(tableName, dbSetting)
@@ -690,6 +702,15 @@ private static string GetBulkInsertSqlText(string tableName,
690702
// End
691703
builder.End();
692704

705+
// SET IDENTITY_INSERT OFF (probably not necessary, but it won't hurt)
706+
if (keepIdentity)
707+
{
708+
builder
709+
.WriteText("SET IDENTITY_INSERT")
710+
.TableNameFrom(tableName, dbSetting)
711+
.WriteText("OFF;");
712+
}
713+
693714
// Return the sql
694715
return builder.ToString();
695716
}
@@ -706,6 +727,7 @@ private static string GetBulkInsertSqlText(string tableName,
706727
/// <param name="hints"></param>
707728
/// <param name="dbSetting"></param>
708729
/// <param name="isReturnIdentity"></param>
730+
/// <param name="keepIdentity"></param>
709731
/// <returns></returns>
710732
private static string GetBulkMergeSqlText(string tableName,
711733
string tempTableName,
@@ -715,7 +737,8 @@ private static string GetBulkMergeSqlText(string tableName,
715737
Field identityField,
716738
string hints,
717739
IDbSetting dbSetting,
718-
bool isReturnIdentity)
740+
bool isReturnIdentity,
741+
bool keepIdentity)
719742
{
720743
// Validate the presence
721744
if (fields?.Any() != true)
@@ -733,7 +756,7 @@ private static string GetBulkMergeSqlText(string tableName,
733756

734757
// Insertable fields
735758
var insertableFields = fields
736-
.Where(field => string.Equals(field.Name, identityField?.Name, StringComparison.OrdinalIgnoreCase) == false);
759+
.Where(field => keepIdentity == true || string.Equals(field.Name, identityField?.Name, StringComparison.OrdinalIgnoreCase) == false);
737760

738761
// Updatable fields
739762
var updateableFields = fields
@@ -743,7 +766,18 @@ private static string GetBulkMergeSqlText(string tableName,
743766
q => string.Equals(q.Name, field.Name, StringComparison.OrdinalIgnoreCase)) == false);
744767

745768
// Compose the statement
746-
builder.Clear()
769+
builder.Clear();
770+
771+
// SET IDENTITY_INSERT ON
772+
if (keepIdentity)
773+
{
774+
builder
775+
.WriteText("SET IDENTITY_INSERT")
776+
.TableNameFrom(tableName, dbSetting)
777+
.WriteText("ON;");
778+
}
779+
780+
builder
747781
// MERGE T USING S
748782
.Merge()
749783
.TableNameFrom(tableName, dbSetting)
@@ -824,6 +858,15 @@ private static string GetBulkMergeSqlText(string tableName,
824858
// End the builder
825859
builder.End();
826860

861+
// SET IDENTITY_INSERT OFF (probably not necessary, but it won't hurt)
862+
if (keepIdentity)
863+
{
864+
builder
865+
.WriteText("SET IDENTITY_INSERT")
866+
.TableNameFrom(tableName, dbSetting)
867+
.WriteText("OFF;");
868+
}
869+
827870
// Return the sql
828871
return builder.ToString();
829872
}

0 commit comments

Comments
 (0)