If you call InsertAllAsync method under TransactionScope, Task InsertAllAsyncInternalBase method fails to start a new transaction and NullReferenceException occurs.
It is used in the following environment.
-.NET Core 3.1
using (var ts = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions(), TransactionScopeAsyncFlowOption.Enabled))
{
var dbConnection = factory.CreateConnection();
if (dbConnection == null)
return;
dbConnection.ConnectionString = connectionStrings;
await dbConnection.EnsureOpenAsync();
var timestamp = DateTimeOffset.UtcNow;
var defaultTimeStamp = new DateTimeOffset(new DateTime(1900, 1, 1));
// OK
await dbConnection.InsertAsync(new User
{
Id = 1,
Name = "user1",
Email = "email",
CreatedAt = timestamp,
UpdatedAt = timestamp,
DeletedAt = defaultTimeStamp
});
// NG Throws System.NullReferenceException
await dbConnection.InsertAllAsync(new[]
{
new User { Id = 2, Name = "user2", Email = "email", CreatedAt = timestamp, UpdatedAt = timestamp, DeletedAt = defaultTimeStamp},
new User { Id = 3, Name = "user3", Email = "email", CreatedAt = timestamp, UpdatedAt = timestamp, DeletedAt = defaultTimeStamp},
new User { Id = 4, Name = "user4", Email = "email", CreatedAt = timestamp, UpdatedAt = timestamp, DeletedAt = defaultTimeStamp},
new User { Id = 5, Name = "user5", Email = "email", CreatedAt = timestamp, UpdatedAt = timestamp, DeletedAt = defaultTimeStamp},
new User { Id = 6, Name = "user6", Email = "email", CreatedAt = timestamp, UpdatedAt = timestamp, DeletedAt = defaultTimeStamp},
new User { Id = 7, Name = "user7", Email = "email", CreatedAt = timestamp, UpdatedAt = timestamp, DeletedAt = defaultTimeStamp},
new User { Id = 8, Name = "user8", Email = "email", CreatedAt = timestamp, UpdatedAt = timestamp, DeletedAt = defaultTimeStamp},
new User { Id = 9, Name = "user9", Email = "email", CreatedAt = timestamp, UpdatedAt = timestamp, DeletedAt = defaultTimeStamp},
new User { Id = 10, Name = "user10", Email = "email", CreatedAt = timestamp, UpdatedAt = timestamp, DeletedAt = defaultTimeStamp}
});
ts.Complete();
await dbConnection.CloseAsync();
}
If you call InsertAllAsync method under TransactionScope, Task InsertAllAsyncInternalBase method fails to start a new transaction and NullReferenceException occurs.
It is used in the following environment.
-.NET Core 3.1