C# TransactionScope - L2E

发布于 2024-08-23 07:15:17 字数 374 浏览 6 评论 0原文

是否值得在 Linq to Entities 上使用 System.Transactions.TransactionScope?

MS 文档上,它说 SQL 在 ObjectContext.SaveChanges 中调用() 全部在内部汇总为一个事务。

我们有 1 个数据库连接,即文件系统上的本地 SQLite 数据库。我们只是想确保对数据库的所有操作都是原子的,我们需要 TransactionScope 吗? 当我们调用某些删除、更新、插入等操作时,我们希望它们全部发生或根本不发生。

Is it worth using System.Transactions.TransactionScope on Linq to Entities?

On the MS documentation, it says that SQL calls within ObjectContext.SaveChanges() are all rolled into one transaction internally.

We have 1 database connection, that is a local SQLite database on the file system. We just want to make sure all our operations to the database are atomic, do we need TransactionScope?
I.E. when we call for some deletes, updates, inserts, etc., we want them all to happen or none at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空心↖ 2024-08-30 07:15:17

乔恩,不,您不需要使用 TransactionScope。乐观并发由 Linq 自动处理。您提供的链接中的代码示例很好地解释了您不必自己回滚事务。我将使用与示例中相同的代码:

    try
    {
        // Try to save changes, which may cause a conflict.
        int num = context.SaveChanges();
        Console.WriteLine("No conflicts. " +
            num.ToString() + " updates saved.");
    }
    catch (OptimisticConcurrencyException)
    {
        // Resolve the concurrency conflict by refreshing the 
        // object context before re-saving changes. 
        context.Refresh(RefreshMode.ClientWins, orders);

        // Save changes.
        context.SaveChanges();
        Console.WriteLine("OptimisticConcurrencyException "
        + "handled and changes saved");
    }

注意刷新、重新保存,这可以解决您的问题。您可以通过从 try 块中抛出异常来测试这一点。

此致

Jon, no you don't need to use TransactionScope. Optimistic concurrency is handled automatically by Linq. The code sample in the link you provide explains that rather well, you don't have to roll back transactions yourself. I would use the same code as in the sample:

    try
    {
        // Try to save changes, which may cause a conflict.
        int num = context.SaveChanges();
        Console.WriteLine("No conflicts. " +
            num.ToString() + " updates saved.");
    }
    catch (OptimisticConcurrencyException)
    {
        // Resolve the concurrency conflict by refreshing the 
        // object context before re-saving changes. 
        context.Refresh(RefreshMode.ClientWins, orders);

        // Save changes.
        context.SaveChanges();
        Console.WriteLine("OptimisticConcurrencyException "
        + "handled and changes saved");
    }

Notice the refresh, re-save, which handles your concern. You could test this out by throwing an exception from within the try block.

Best Regards

旧时光的容颜 2024-08-30 07:15:17

如果您想在单个事务中包含 ObjectContext.SaveChanges 以外的内容(例如读取要更改的数据以及更改),那么您需要使用 TransactionScope

If you want to include more than the ObjectContext.SaveChanges in a single transaction (e.g. reads of data you are going to change, as well as the changes) then you need to make use of TransactionScope.

酸甜透明夹心 2024-08-30 07:15:17

如果您需要执行理查德所说的操作(尽管看起来不太可能),您可以使用以下代码:

TransactionManager transactionManager = null;

try
{
    bool isBorrowedTransaction = ConnectionScope.Current.HasTransaction;
    transactionManager = ConnectionScope.ValidateOrCreateTransaction(true);

    //MANY SAVES

    if (!isBorrowedTransaction && transactionManager != null && transactionManager.IsOpen)
        transactionManager.Commit();
}
catch (Exception ex)
{
    if (transactionManager != null && transactionManager.IsOpen)
        transactionManager.Rollback();
    log.Error("An unexpected Exception occurred", ex);
    throw;
}

You could use the following code if you need to do what Richard says (though it seems rather unlikely):

TransactionManager transactionManager = null;

try
{
    bool isBorrowedTransaction = ConnectionScope.Current.HasTransaction;
    transactionManager = ConnectionScope.ValidateOrCreateTransaction(true);

    //MANY SAVES

    if (!isBorrowedTransaction && transactionManager != null && transactionManager.IsOpen)
        transactionManager.Commit();
}
catch (Exception ex)
{
    if (transactionManager != null && transactionManager.IsOpen)
        transactionManager.Rollback();
    log.Error("An unexpected Exception occurred", ex);
    throw;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文