Frosty840
Frosty840

Reputation: 8343

Using Transaction with ExecuteMethodCall

Right...

I have a project, left for me by my illustrious predecessor, parts of which which appear to have originally been auto-generated using features of LINQ. Unfortunately, instead of actually learning how to use and configure LINQ properly, he appears to have renamed all of the auto-generated files and butchered them wherever he saw fit to make changes.

I'm not mentioning this to complain (specifically), but to provide context that, if advised, for example, to configure some aspect of the code in the Designer, that the Designer for this code no longer exists, because it's been ripped out.

Anyway, I have a particular function, which I believe was originally designer-generated, which reads as follows:

<System.Data.Linq.Mapping.DatabaseAttribute(Name:="ProductionControlMS")> _
Partial Public Class Database
Inherits System.Data.Linq.DataContext

<FunctionAttribute(Name:="dbo.ReleaseItemToProduction")> _
Public Function ReleaseBuildPack(<Parameter(Name:="TransitionID", DbType:="Int")> ByVal TransitionID As Integer, _
                                          <Parameter(Name:="SubLevelID", DbType:="Int")> ByVal SubLevelID As Integer, _
                                          <Parameter(Name:="CompNo", DbType:="TinyInt")> ByVal CompNo As Byte) As Integer
    Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, CType(MethodInfo.GetCurrentMethod, MethodInfo), TransitionID, SubLevelID, CompNo)
    Return CType(result.ReturnValue, Integer)
End Function

I need to be able to assign put this function into a transaction, as I may need to roll it back. Unfortunately, while I have been able to call ProductionControlMS.Connection.BeginTransaction I can't see how to assign the transaction to the current command. So, when the code gets to the Me.ExecuteMethodCall line, I get an InvalidOperationException, with the message:

ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.

What can I get hold of, here, in order to assign a transaction to it, to allow me to rollback these actions?

Upvotes: 0

Views: 534

Answers (2)

StuartLC
StuartLC

Reputation: 107297

You can use TransactionScopes to control the transactions, without needing to directly hook into LINQ's SQLConnection at all. (LINQ2SQL will hook into the TransactionScope automatically)

For example:

Using ts As New TransactionScope()
    new MyDataContext().ReleaseBuildPack(TransitionID, SubLevelID, CompNo )

    ' Do whatever else you need to do here under the same transaction

    ' If successful, then Complete. 
    ' If no Complete() is called before the ts exits scope then the transaction will be rolled back
    ts.Complete()
End Using 

Upvotes: 1

smartcaveman
smartcaveman

Reputation: 42256

You can get the change set from the data context and use the change set data to revert changes.

An article about this approach is available at http://xacc.wordpress.com/2009/02/17/datacontextrevertchanges/

Upvotes: 0

Related Questions