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

Created on 27 Jul 2020  路  4Comments  路  Source: mikependon/RepoDB

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();
}
Fixed bug deployed priority todo

All 4 comments

I had replicated this bug on the Core line. It is very unfortunate, we had removed it when we migrated from the old code line (see this line in to the new code line (see this line).

The fix will be issued on the new release together with MergeAll and UpdateAll methods (to all supported RDBMS). Mind if you can share when do you need this? Thanks

@karuakun - this bug is now fixed and is a part of the latest deployment (RepoDb.MySqlConnector (v1.0.4)). Would you be able to validate in your side? Thanks

> Install-Package RepoDb.MySqlConnector -version 1.0.4

Thanks for fixing it!
I confirmed that it was fixed.

Closing this ticket now. Thanks

Was this page helpful?
0 / 5 - 0 ratings