Base solution for your next web application
Open Closed

The specified transaction is not associated with the current connection. Only transactions associated with the current connection may be used. #11224


User avatar
0
hra created

Hi,

AspNetZero Angular+Core: API: v10.0.0 | Client: v10.0.0 (20220815)

I have scanned the forum, and found older discussions relating to this error, such as: https://support.aspnetzero.com/QA/Questions/4162/transaction-is-not-associated-with-the-current-connection

The solutions proposed do not work for me.

My scenario is that I have the ANZ dbcontext, and I also have a second DbContext. It works at regular runtime, the above error only happens during unit testing.

I have this in both my DbContexts:

Configuration.ReplaceService<IEfCoreTransactionStrategy, DbContextEfCoreTransactionStrategy>(DependencyLifeStyle.Transient);

And the error occurs when testing an AppService which attempts to resolve my custom DbContext

        IDbContextProvider<CustomDbContext> _customDbContextProvider
        
        public CustomDbContext DbContext => _customDbContextProvider.GetDbContext(); // error happens here

Yes, turning off transactions fixes the issue - however, my code needs to be transactional.

Thanks,


5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @hra

    Is it possible to share your project with us via email so we can check this problem ?

  • User Avatar
    0
    hra created

    Hi,

    It would take a while to see if I could get clearance for this. However, I know I could set up a Teams video session with you, and show you the issue / let you drive a debug session?

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @hra

    This might be an issue hard to detect. Is it possible to reproduce this on a non-modified AspNet Zero project by adding a second DbContext and writing a unit test ? If so, could you share the steps you applied ?

  • User Avatar
    0
    hra created

    Hi,

    Thanks for the offer - interestingly enough, as I have written more infrastructure to support multiple databases (see my other post about how to integration test 2 different database schemas in the same in-memory db file, with some shared entities) - the issue has now gone away. (yay).

    Not wanting to derail the subject too much - I have noticed that UnitOfWorkManager.Current appears to be null within all unit tests. Is this not set up by the Abp test base classes somewhere by default? Or are we supposed to do this ourselves?

    This would imply that none of the existing tests call methods which use the UnitOfWorkManager - which is surprising - perhaps I'm wrong... but it's definitely null for me - which is breaking some tests.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @hra

    Not wanting to derail the subject too much - I have noticed that UnitOfWorkManager.Current appears to be null within all unit tests. Is this not set up by the Abp test base classes somewhere by default? Or are we supposed to do this ourselves?

    Unit test methods are not unit of work by default, so if you need a unit of work, you need to start it manually.