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?
我想添加到数据库中,在这种情况下,大量的信息,在我的LINQ到SQL模型中没有事务。是否有人知道如何将LINQ关闭到datacontext上的SQL事务或提交更改操作?
4 个解决方案
#1
2
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.
就我所知,如果不将数据库的恢复模式从full改为simple,就无法在insert上禁用事务。
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.
如果您可以将批处理大小(我知道您可以使用NHibernate,而不是LINQ到SQL)配置为100或更多,那么也可以减少对数据库的往返,这也将提高插入性能。
#2
2
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.
既然LINQ to SQL将创建一个不存在的事务,那么您所能做的最好的事情就是创建一个您自己可以接受的隔离级别的事务。
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 {}
}
}
#3
0
Can't you just set the Transaction property on DataContext to null?
难道不能将DataContext上的事务属性设置为null吗?
#4
0
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.
将ConflictMode设置为ContinueOnConflict,您将能够成功地提交任何有效的更改,并且您将拥有一个列表,除了您无法跟踪的更改之外。
db.SubmitChanges(ConflictMode.ContinueOnConflict)
I've got a reference to using this technique to stop errors backing out your submit changes: SandKeySoftware Linq Tutorial
我有一个关于使用这种技术来阻止错误退出您的提交更改的参考资料:SandKeySoftware Linq教程
If you have distributed transactions occurring, this should help too:
如果您已经进行了分布式事务,这也会有所帮助:
using(var scope = new TransactionScope(TransactionScopeOption.Suppress))
{
//linqy stuff here
}
#1
2
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.
就我所知,如果不将数据库的恢复模式从full改为simple,就无法在insert上禁用事务。
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.
如果您可以将批处理大小(我知道您可以使用NHibernate,而不是LINQ到SQL)配置为100或更多,那么也可以减少对数据库的往返,这也将提高插入性能。
#2
2
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.
既然LINQ to SQL将创建一个不存在的事务,那么您所能做的最好的事情就是创建一个您自己可以接受的隔离级别的事务。
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 {}
}
}
#3
0
Can't you just set the Transaction property on DataContext to null?
难道不能将DataContext上的事务属性设置为null吗?
#4
0
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.
将ConflictMode设置为ContinueOnConflict,您将能够成功地提交任何有效的更改,并且您将拥有一个列表,除了您无法跟踪的更改之外。
db.SubmitChanges(ConflictMode.ContinueOnConflict)
I've got a reference to using this technique to stop errors backing out your submit changes: SandKeySoftware Linq Tutorial
我有一个关于使用这种技术来阻止错误退出您的提交更改的参考资料:SandKeySoftware Linq教程
If you have distributed transactions occurring, this should help too:
如果您已经进行了分布式事务,这也会有所帮助:
using(var scope = new TransactionScope(TransactionScopeOption.Suppress))
{
//linqy stuff here
}