Rollbackを呼び出す必要はありません usingを使用しているため手動で 声明。
DbContextTransaction.Dispose usingの最後にメソッドが呼び出されます ブロック。また、トランザクションが正常にコミットされなかった場合(呼び出されなかったり、例外が発生したりしない場合)、トランザクションは自動的にロールバックされます。以下は、SqlInternalTransaction.Disposeのソースコードです。 メソッド(DbContextTransaction.Dispose SqlServerプロバイダーを使用すると、最終的にそれに委任されます):
private void Dispose(bool disposing)
{
// ...
if (disposing && this._innerConnection != null)
{
this._disposing = true;
this.Rollback();
}
}
ほら、_innerConnectionかどうかをチェックします nullでない場合は、トランザクションをロールバックします(コミットされている場合は、_innerConnection nullになります)。 Commitを見てみましょう する:
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);
}
}