本机 Entity Framework 交易行为
Native Entity Framework Transactions Behavior
我读了一些文章,比如 this 一篇,他们说:
In all versions of Entity Framework, whenever you execute
SaveChanges() to insert, update or delete on the database the
framework will wrap that operation in a transaction. This transaction
lasts only long enough to execute the operation and then completes.
When you execute another such operation a new transaction is started.
但是我无法从这个描述中理解当我要执行多个更改时 EF 的行为。它是将所有 insert
、update
或 delete
包装成一个 transaction
,还是每个人都有自己的 transaction
?文中的术语“那个操作”既可以是整个SaveChanges()
操作,也可以是单独的insert
、update
或delete
.
我的问题的一个实际例子是:
using(var context = new MyContext())
{
context.MyClass1.Add(myObject1);
var myObject2 = context.MyClass2.Single();
myObject2.Value1 = 10;
context.SaveChanges();
}
我希望如果将 myObject2.Value1
设置为 10
时出现任何错误,整个操作将被取消,因此 myObject1
也不会添加到数据库中,反之亦然相反。
Does it wrap all the insert, update or delete into one transaction, or each one of them get it's own transaction?
在您实例化上下文直到到达 SaveChanges() 方法期间,所有插入、更新和更新操作都在单个事务中执行,因此作为对数据库服务器的原子操作。如果您的操作之一失败,则在到达失败操作之前成功执行的所有操作都会回滚。但是你的实体仍然会有你对它们所做的修改。只有数据库服务器会拒绝更改。
The term "that operation" in the text can be both the whole SaveChanges() operation or the individual insert, update or delete.
表示漏洞SaveChanges
为ACID(原子性、一致性、隔离性、持久性)操作。
I would like that if there are any errors setting myObject2.Value1 to 10, the whole operation is canceled, so myObject1 isn't added to the DB as well and vice versa.
您的对象修改不会添加到数据库中,但对象在您的应用程序中的状态不会丢失。如果您检查此 属性.
的值,值 10 仍将作为 Value1
的值存在
我读了一些文章,比如 this 一篇,他们说:
In all versions of Entity Framework, whenever you execute SaveChanges() to insert, update or delete on the database the framework will wrap that operation in a transaction. This transaction lasts only long enough to execute the operation and then completes. When you execute another such operation a new transaction is started.
但是我无法从这个描述中理解当我要执行多个更改时 EF 的行为。它是将所有 insert
、update
或 delete
包装成一个 transaction
,还是每个人都有自己的 transaction
?文中的术语“那个操作”既可以是整个SaveChanges()
操作,也可以是单独的insert
、update
或delete
.
我的问题的一个实际例子是:
using(var context = new MyContext())
{
context.MyClass1.Add(myObject1);
var myObject2 = context.MyClass2.Single();
myObject2.Value1 = 10;
context.SaveChanges();
}
我希望如果将 myObject2.Value1
设置为 10
时出现任何错误,整个操作将被取消,因此 myObject1
也不会添加到数据库中,反之亦然相反。
Does it wrap all the insert, update or delete into one transaction, or each one of them get it's own transaction?
在您实例化上下文直到到达 SaveChanges() 方法期间,所有插入、更新和更新操作都在单个事务中执行,因此作为对数据库服务器的原子操作。如果您的操作之一失败,则在到达失败操作之前成功执行的所有操作都会回滚。但是你的实体仍然会有你对它们所做的修改。只有数据库服务器会拒绝更改。
The term "that operation" in the text can be both the whole SaveChanges() operation or the individual insert, update or delete.
表示漏洞SaveChanges
为ACID(原子性、一致性、隔离性、持久性)操作。
I would like that if there are any errors setting myObject2.Value1 to 10, the whole operation is canceled, so myObject1 isn't added to the DB as well and vice versa.
您的对象修改不会添加到数据库中,但对象在您的应用程序中的状态不会丢失。如果您检查此 属性.
的值,值 10 仍将作为Value1
的值存在