Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
borisdj committed Mar 12, 2024
2 parents 932eefb + a32d8ec commit 7de0593
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
15 changes: 6 additions & 9 deletions EFCore.BulkExtensions/DbContextBulkTransactionSaveChanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Transactions;

namespace EFCore.BulkExtensions;

Expand Down Expand Up @@ -91,16 +92,12 @@ private static async Task SaveChangesAsync(DbContext context, BulkConfig? bulkCo
}
var connection = context.GetUnderlyingConnection(bulkConfig);

bool doExplicitCommit = false;
if (context.Database.CurrentTransaction == null)
{
doExplicitCommit = true;
}
var hasExistingTransaction = context.Database.CurrentTransaction != null || Transaction.Current != null;

try
{

var transaction = context.Database.CurrentTransaction ?? context.Database.BeginTransaction();
var transaction = hasExistingTransaction ? null : context.Database.CurrentTransaction ?? context.Database.BeginTransaction();

if (option == 1)
{
Expand Down Expand Up @@ -207,15 +204,15 @@ private static async Task SaveChangesAsync(DbContext context, BulkConfig? bulkCo
}
}
}
if (doExplicitCommit)
if (!hasExistingTransaction)
{
transaction.Commit();
transaction!.Commit();
context.ChangeTracker.AcceptAllChanges();
}
}
finally
{
if (doExplicitCommit)
if (!hasExistingTransaction)
{

if (isAsync)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public static string CountUniqueConstrain(TableInfo tableInfo)
bool usePG_Catalog = true; // PG_Catalog used instead of Information_Schema
if (usePG_Catalog)
{
q = @"SELECT COUNT(*)
q = @"SELECT COUNT(distinct c.conname)
FROM pg_catalog.pg_namespace nr,
pg_catalog.pg_class r,
pg_catalog.pg_attribute a,
Expand Down

0 comments on commit 7de0593

Please sign in to comment.