简体   繁体   English

EF4中的TransactionScope帮助

[英]Help with TransactionScope in EF4

I am new to TransactionScope. 我是TransactionScope的新手。 I Just want to make sure what i am doing is correct in the code below. 我只是想确保我在做什么在下面的代码中是正确的。

private void DoSomeWork()
{
    using (var context = new MyEntities())
    using (TransactionScope scope = new TransactionScope())
    {
        context.Connection.Open();

        int ID = context.CallUpdateStoredProc();

        RecursiveDelete((EntityConnection)context.Connection, ID);

        context.Connection.Close();

        scope.Complete(); 
    }
}

private void RecursiveDelete(EntityConnection connection,int someID)
{
    using (var context = new MyEntities(connection))
    {
        var UpdatedLinks = context.CallSaveStoredProc(someID).ToList();

        foreach (int UpdatedLink in UpdatedLinks)
        {
            RecursiveDelete(connection,UpdatedLink);     
        }
    }
}

Entity Framework 4 is TransactionScope aware, so your example will work fine. 实体框架4可以识别TransactionScope,因此您的示例可以正常工作。 See this example on MSDN . 请参阅MSDN上的此示例

In terms of how you are using the TransactionScope class , yes, it is correct, you generally use the pattern of: 就您如何使用TransactionScope类而言 ,是的,这是正确的,通常使用以下模式:

// Use overloads of constructor if you need to fine-tune the transaction
using (var tx = new TransactionScope())
{ 
    // Do your work.

    // Call Complete if the work is truly complete.
    // Note, if you determine through logic, you don't
    // have to call Complete.
    tx.Complete();
}

Once you call the Complete method , it will indicate that the curent Transaction is ready to be committed. 一旦调用Complete方法 ,它将指示当前Transaction已准备就绪,可以提交。 Note that the transaction isn't committed at the time of the call to Complete , but when the TransactionScope is disposed. 请注意,在调用Complete时不会提交TransactionScope ,而是在TransactionScope时提交。

From the documentation for TransactionScope.Complete : TransactionScope.Complete的文档中:

The actual work of commit between the resources manager happens at the End Using statement if the TransactionScope object created the transaction. 如果TransactionScope对象创建了事务,则资源管理器之间的实际提交工作将在End Using语句中进行。

Also, as indicated in the comments in the sample code, it's a conscious decision whether or not the call to Complete should be made. 此外,如示例代码中的注释所示,是否应该调用Complete也是一个明智的决定。

Again, from the documentation for TransactionScope.Complete (emphasis mine): 同样,从TransactionScope.Complete的文档中(强调我的意思):

When you are satisfied that all operations within the scope are completed successfully, you should call this method only once to inform that transaction manager that the state across all resources is consistent, and the transaction can be committed. 如果您对范围内的所有操作都成功完成感到满意 ,则应该只调用一次此方法,以通知事务管理器所有资源的状态是一致的,并且可以提交事务。

Now, as for whether or not TransactionScope can be used with the Entity Framework, the answer is yes, as the documentation on MSDN titled "How to: Manage Transactions in the Entity Framework" indicates. 现在,关于是否可以将TransactionScope与实体框架一起使用,答案是肯定的,正如MSDN上名为“如何:在实体框架中管理事务”的文档所述

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

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