Base solution for your next web application
Open Closed

Get SQLConnection and Transaction for UOW #4761


User avatar
0
bertusvanzyl created

I have a custom Repository. Everything is working fine, but I have a small issue I am unable to solve.

Currently I get the SqlConnection by injecting IDbContextProvider, and then this.DbContextProvider.GetDbContext().Database.Connection;

The problem is that I would like to use the current transaction on the UOW as well, if there is one.

I did see in thispost ([https://forum.aspnetboilerplate.com/viewtopic.php?f=5&t=9725&p=21656&hilit=Uow+connection#p21656])) that I can inject IActiveTransactionProvider.

However, I could not find any documentation on the ActiveTransactionProviderArgs parameter. Is there a guide or documentation somewhere I can consult?

Or will the

{"ContextType", typeof(YourAppDbContext) },
                {"MultiTenancySide", MultiTenancySide }

parameter always work for me?


2 Answer(s)
  • User Avatar
    0
    bertusvanzyl created

    In the meantime I tried experimenting a bit.

    Getting the connection from IActiveTransactionProvider works:

    var connection = (SqlConnection) this.ActiveTransactionProvider.GetActiveConnection(new ActiveTransactionProviderArgs
                {
                    {"ContextType", typeof(PCMDbContext) },
                    {"MultiTenancySide", MultiTenancySide }
                });
    

    But getting the transaction with the same parameters throws an exception.

    this.ActiveTransactionProvider.GetActiveTransaction(new ActiveTransactionProviderArgs
                    {
                        {"ContextType", typeof(PCMDbContext)},
                        {"MultiTenancySide", MultiTenancySide}
                    });
    

    Gives me an exception from inside GetActiveTransaction:

    System.NullReferenceException: Object reference not set to an instance of an object. at Abp.EntityFramework.EfActiveTransactionProvider.GetActiveTransaction(ActiveTransactionProviderArgs args)

  • User Avatar
    0
    ismcagdas created
    Support Team

    @BertusVanZyl since you have the dbContext, can you try like below;

    var transaction =  this.DbContextProvider.GetDbContext().Database.CurrentTransaction?.GetDbTransaction();