आपको 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 शून्य नहीं है, यदि नहीं, तो लेन-देन को रोलबैक करें (यदि प्रतिबद्ध है, _innerConnection शून्य होगा)। आइए देखें क्या 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);
}
}