问题描述
使用EF6你有可以像使用一个新的事务:
With EF6 you have a new transaction which can be used like:
using (var context = new PostEntityContainer())
{
using (var dbcxtransaction = context.Database.BeginTransaction())
{
try
{
PostInformation NewPost = new PostInformation()
{
PostId = 101,
Content = "This is my first Post related to Entity Model",
Title = "Transaction in EF 6 beta"
};
context.Post_Details.Add(NewPost);
context.SaveChanges();
PostAdditionalInformation PostInformation = new PostAdditionalInformation()
{
PostId = (101),
PostName = "Working With Transaction in Entity Model 6 Beta Version"
};
context.PostAddtional_Details.Add(PostInformation);
context.SaveChanges();
dbcxtransaction.Commit();
}
catch
{
dbcxtransaction.Rollback();
}
}
}
时回滚真正需要的东西的时候横着走?我很好奇,因为提交描述说:提交的底层存储事务
Is rollback actually needed when things go sideways? I'm curious because the Commit description says: "Commits the underlying store transaction."
而回滚介绍说:回滚底层存储事务
Whereas the Rollback description says: "Rolls back the underlying store transaction."
这让我好奇,因为在我看来,如果提交不叫,在previously执行的命令不会被保存(这似乎是合乎逻辑的我)。但是,如果是这样的话,会是什么原因被调用的回退功能?在EF5我用它不具有这似乎合乎逻辑的我回滚功能(仅完成)的TransactionScope。由于MS DTC的原因,我不能使用的TransactionScope了,但我也不能使用尝试捕捉像上面的例子(即,我只需要提交)。
This makes me curious, because it looks to me that if Commit isn't called, the previously executed commands will not be stored (which seems logical to me). But if that is the case, what would the reason be to call the Rollback function? In EF5 I used TransactionScope which didn't have a Rollback function (only a Complete) which seemed logical to me. Due to MS DTC reasons I cannot use the TransactionScope anymore, but I also cannot use a try catch like the example above (i.e., I only need the Commit).
推荐答案
您不必因为您使用的是还原
手动调用使用
语句。
You don't need to call Rollback
manually because you are using the using
statement.
DbContextTransaction.Dispose
方法将在使用
块的末尾调用。它会自动回滚如果交易没有成功提交(不叫或遇到例外)交易。以下是源$ C $ C SqlInternalTransaction.Dispose
办法( DbContextTransaction.Dispose
使用时,最终将委托给它SqlServer的提供者):
DbContextTransaction.Dispose
method will be called in the end of the using
block. And it will automatically rollback the transaction if the transaction is not successfully committed (not called or encountered exceptions). Following is the source code of SqlInternalTransaction.Dispose
method (DbContextTransaction.Dispose
will finally delegate to it when using SqlServer provider):
private void Dispose(bool disposing)
{
// ...
if (disposing && this._innerConnection != null)
{
this._disposing = true;
this.Rollback();
}
}
您看,它会检查是否 _innerConnection
不为空,如果没有,回滚事务(如果实施, _innerConnection
为null)。让我们来看看提交
所做的:
You see, it checks if _innerConnection
is not null, if not, rollback the transaction (if committed, _innerConnection
will be null). Let's see what Commit
does:
internal void Commit()
{
// Ignore many details here...
this._innerConnection.ExecuteTransaction(...);
if (!this.IsZombied && !this._innerConnection.IsYukonOrNewer)
{
// Zombie() method will set _innerConnection to null
this.Zombie();
}
else
{
this.ZombieParent();
}
// Ignore many details here...
}
internal void Zombie()
{
this.ZombieParent();
SqlInternalConnection innerConnection = this._innerConnection;
// Set the _innerConnection to null
this._innerConnection = null;
if (innerConnection != null)
{
innerConnection.DisconnectTransaction(this);
}
}
这篇关于实体框架6事务回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!