Skip to content

Bug: NullReferenceException occurs when InsertAllAsync is executed on Connection that belongs to TransactionScope. #490

@karuakun

Description

@karuakun

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

  • RepoDb.MysqlConnector
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();
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingdeployedFeature or bug is deployed at the current releasefixedThe bug, issue, incident has been fixed.priorityTop priority feature or things to dotodoThings to be done in the future

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions