如何禁用 LINQ to Sql 事务?

发布于 2024-08-13 21:25:58 字数 91 浏览 3 评论 0原文

我想要向数据库添加内容,在本例中是大量信息,但我的 LINQ To SQL 模型上没有事务。有谁知道如何关闭数据上下文或提交更改操作上的 LINQ To SQL 事务?

I want to make additions to a database, in this case massive amounts of information, with no transactions on my LINQ To SQL model. Does anyone know how to turn off LINQ To SQL transactions on a datacontext or on the submit changes operation?

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

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

发布评论

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

评论(4

简单爱 2024-08-20 21:25:58

据我所知,如果不将数据库的恢复模式从完整恢复到简单,就无法禁用插入事务。

但要小心,因为您只能使用简单恢复模式将数据库恢复到最新的备份,并且无法应用自执行备份以来发生的事务中的事务日志。

如果您可以将批处理大小配置为 100 或更多(我知道您可以使用 NHibernate,但对 LINQ to SQL 不那么肯定),这也可以减少数据库的往返次数,从而提高插入性能。

As far as I can tell, there is no way to disable transactions on insert without changing the recovery mode of the database from full to to simple.

Be careful though, since you can only recover a database with a simple recovery mode to the latest backup and won't be able to apply the transaction logs from transactions that occurred since that backup was performed.

If you can configure a batch size (I know you can with NHibernate, not positive about LINQ to SQL) to something like 100 or more, that can also reduce the round trips to the database which will also increase insert performance.

毁梦 2024-08-20 21:25:58

由于 LINQ to SQL 将创建一个事务(如果事务不存在) ,您能做的最好的事情就是使用您可以接受的隔离级别创建自己的事务。

Transaction transaction = new Transaction( IsolationLevel.Chaos );
try
{
    using (var dc = new MyDataContext())
    {
       dc.Transaction = transaction;
       ...
       transaction.Commit();
    }
}
catch
{
    transaction.Rollback();
}

或者,您可以提交每个更改,忽略失败。您仍然会得到一笔交易,但它只包含每个单独的更改。

foreach (var foo in foos)
{
    using (var context = new MyDataContext())
    {
        context.Foos.InsertOnSubmit(foo);
        try
        {
            context.SubmitChanges();
        }
        catch {}
    }
}

Since LINQ to SQL will create a transaction if one doesn't exist, the best you can do is create your own transaction with an isolation level that is acceptable to you.

Transaction transaction = new Transaction( IsolationLevel.Chaos );
try
{
    using (var dc = new MyDataContext())
    {
       dc.Transaction = transaction;
       ...
       transaction.Commit();
    }
}
catch
{
    transaction.Rollback();
}

Alternatively, you could submit on each change, ignoring failures. You still get a transaction, but it only wraps each individual change.

foreach (var foo in foos)
{
    using (var context = new MyDataContext())
    {
        context.Foos.InsertOnSubmit(foo);
        try
        {
            context.SubmitChanges();
        }
        catch {}
    }
}
挽容 2024-08-20 21:25:58

不能将 DataContext 上的 Transaction 属性设置为 null 吗?

Can't you just set the Transaction property on DataContext to null?

鸢与 2024-08-20 21:25:58

将 ConflictMode 设置为ContinueOnConflict,您将能够成功提交任何有效的更改,并且您将获得一个列表,其中列出了您无法成功跟进的更改。

db.SubmitChanges(ConflictMode.ContinueOnConflict)

我有一个关于使用此技术来阻止错误撤销提交更改的参考:SandKeySoftware Linq 教程

如果您发生分布式事务,这也应该有帮助:

using(var scope = new TransactionScope(TransactionScopeOption.Suppress))
{
    //linqy stuff here
}

Set the ConflictMode to ContinueOnConflict and you will be able to commit successfully any changes that work, and you'll have a list at the exception of changes that weren't successful for you to follow up.

db.SubmitChanges(ConflictMode.ContinueOnConflict)

I've got a reference to using this technique to stop errors backing out your submit changes: SandKeySoftware Linq Tutorial

If you have distributed transactions occurring, this should help too:

using(var scope = new TransactionScope(TransactionScopeOption.Suppress))
{
    //linqy stuff here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文