简体   繁体   English

通过ExecuteMethodCall使用事务

[英]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. 我有一个杰出的前任留给我的项目,其中的某些部分最初似乎是使用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. 不幸的是,他似乎没有真正学习如何正确使用和配置LINQ,而是重命名了所有自动生成的文件,并在他认为适合进行更改的任何地方对其进行了屠宰。

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. 不幸的是,虽然我已经能够调用ProductionControlMS.Connection.BeginTransaction ,但是我看不到如何将事务分配给当前命令。 So, when the code gets to the Me.ExecuteMethodCall line, I get an InvalidOperationException, with the message: 因此,当代码到达Me.ExecuteMethodCall行时,我收到一个InvalidOperationException并显示以下消息:

ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. 当分配给该命令的连接处于未决本地事务中时,ExecuteNonQuery要求该命令具有事务。 The Transaction property of the command has not been initialized. 该命令的Transaction属性尚未初始化。

What can I get hold of, here, in order to assign a transaction to it, to allow me to rollback these actions? 在这里,我可以掌握什么以便为其分配事务,以便回滚这些操作?

You can use TransactionScopes to control the transactions, without needing to directly hook into LINQ's SQLConnection at all. 您可以使用TransactionScopes来控制事务,而根本不需要直接挂接到LINQ的SQLConnection。 (LINQ2SQL will hook into the TransactionScope automatically) (LINQ2SQL将自动挂接到TransactionScope中)

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 

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/ 有关此方法的文章,请访问http://xacc.wordpress.com/2009/02/17/datacontextrevertchanges/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM