Search code examples
c#.netsqltransactiontransactionmanager

C# TransactionManager


Hi I have a generator which made some automated code (creating all the get, set, etc) for me structured by a Database, in this code there is a piece of code that is as follows:

SqlTransaction transaction = TransactionManager.GetTransaction();

I have added the reference and imported using System.Transactions; at the beginning of the file manually because the code generator never did this. Once imported, this removed the TransactionManager error I was getting but now I am receiving an error on .GetTransaction(); from what I have researched there is no such method in the TransactionManager.
Does anyone have any clue as to what should be in place of this code (the generator has some code which has to be replaced due to errors, my guess is this is one of those cases...). Any suggestions would be appreciated! Thanks in advance!


Solution

  • This answer presumes that the generator you are using was built in house and you are modifying it now - if it was an external code generator then you should check the manual for it around what references you need.

    The TransactionManager class in System.Transactions does't actually create or enlist in transactions for you. It simply provides information about the current transactions. From MSDN:

    This class contains static properties and methods used to obtain information about the default transaction options.

    Instead I think you want either:

    Googling TransactionManager it appears that people commonly write their own classes with this name which do things like offer a GetTransaction() method - possible this is why the generator has this name?