事务上下文在添加对象时有什么区别?

时间:2022-03-14 02:35:22

Does TransactionScope make any difference when there is just one insert in the table?

当表中只有一个insert时,TransactionScope会有什么不同吗?

Is

MyObjectContext.Messages.Save( message, m => m.ID == message.ID);
MyObjectContext.SaveChanges();

any different from

任何不同于

using( var ts = new TransactionScope() )
{
    MyObjectContext.Messages.Save( message, m => m.ID == message.ID);
    MyObjectContext.SaveChanges();
    ts.Complete();
}

and how exactly?

和如何?

3 个解决方案

#1


1  

There is a difference. If you use just SaveChanges you still have a transaction but it has default isolation level for database server - in case of SQL server it is Read committed. If you use TransactionScope with default configuration you have Serialized transaction isolation level but you can change it if you use other constructor of TransactionScope.

有一个区别。如果您只使用SaveChanges,那么您仍然有一个事务,但是它对于数据库服务器具有默认的隔离级别——对于SQL server,它被读取为已提交。如果您使用默认配置的TransactionScope,那么您有序列化的事务隔离级别,但是如果您使用TransactionScope的其他构造函数,您可以更改它。

So it makes a difference if you need control over transaction isolation level.

因此,如果您需要对事务隔离级别进行控制,就会产生不同的结果。

#2


1  

It doesn't matter if you are saving one item or multiple items, the use of a TransactionScope is redundant here.

保存一个项目或多个项目并不重要,在这里使用TransactionScope是多余的。

From the documentation for ObjectContext.SaveChanges:

从ObjectContext.SaveChanges的文档中:

SaveChanges operates within a transaction. SaveChanges will roll back that transaction and throw an exception if any of the dirty ObjectStateEntry objects cannot be persisted.

SaveChanges在事务中操作。SaveChanges将回滚该事务,并在无法持久化任何脏ObjectStateEntry对象时抛出异常。

So you are layering on a TransactionScope in your example with no added benefit.

因此,在您的示例中,您是在事务范围上分层,没有附加的好处。

Now, if you had two separate ObjectContext instances with separate sets of data that you wanted to ensure that both were saved, then you would absolutely need the TransactionScope around both calls to ObjectContext.SaveChanges.

现在,如果您有两个独立的ObjectContext实例,其中包含您想要确保两者都被保存的数据集,那么您绝对需要在对ObjectContext. savechanges的两个调用周围使用TransactionScope。

#3


1  

No, there is no difference. SaveChanges operates within a transaction. SaveChanges will roll back that transaction and throw an exception if any of the dirty ObjectStateEntry objects cannot be persisted.

不,没有区别。SaveChanges在事务中操作。SaveChanges将回滚该事务,并在无法持久化任何脏ObjectStateEntry对象时抛出异常。

#1


1  

There is a difference. If you use just SaveChanges you still have a transaction but it has default isolation level for database server - in case of SQL server it is Read committed. If you use TransactionScope with default configuration you have Serialized transaction isolation level but you can change it if you use other constructor of TransactionScope.

有一个区别。如果您只使用SaveChanges,那么您仍然有一个事务,但是它对于数据库服务器具有默认的隔离级别——对于SQL server,它被读取为已提交。如果您使用默认配置的TransactionScope,那么您有序列化的事务隔离级别,但是如果您使用TransactionScope的其他构造函数,您可以更改它。

So it makes a difference if you need control over transaction isolation level.

因此,如果您需要对事务隔离级别进行控制,就会产生不同的结果。

#2


1  

It doesn't matter if you are saving one item or multiple items, the use of a TransactionScope is redundant here.

保存一个项目或多个项目并不重要,在这里使用TransactionScope是多余的。

From the documentation for ObjectContext.SaveChanges:

从ObjectContext.SaveChanges的文档中:

SaveChanges operates within a transaction. SaveChanges will roll back that transaction and throw an exception if any of the dirty ObjectStateEntry objects cannot be persisted.

SaveChanges在事务中操作。SaveChanges将回滚该事务,并在无法持久化任何脏ObjectStateEntry对象时抛出异常。

So you are layering on a TransactionScope in your example with no added benefit.

因此,在您的示例中,您是在事务范围上分层,没有附加的好处。

Now, if you had two separate ObjectContext instances with separate sets of data that you wanted to ensure that both were saved, then you would absolutely need the TransactionScope around both calls to ObjectContext.SaveChanges.

现在,如果您有两个独立的ObjectContext实例,其中包含您想要确保两者都被保存的数据集,那么您绝对需要在对ObjectContext. savechanges的两个调用周围使用TransactionScope。

#3


1  

No, there is no difference. SaveChanges operates within a transaction. SaveChanges will roll back that transaction and throw an exception if any of the dirty ObjectStateEntry objects cannot be persisted.

不,没有区别。SaveChanges在事务中操作。SaveChanges将回滚该事务,并在无法持久化任何脏ObjectStateEntry对象时抛出异常。