What does the Rollback method in EF Core do?
-
Hi, RollBack method; Roll back whatever changes have been made to the database. Following example I didn't use Commit(), what will RollBack() do? What exactly did transaction.Rollback() do?
using (var context = new AppDbContext())
{
using (var transaction = context.Database.BeginTransaction())
{
try
{
var myObjectOne = new MyObjectOne() { Name = "Book" };
context.MyObjectOnes.Add(myObjectOne);
context.SaveChanges();var myVal = myObjectOne.Id \* 3.14; //Suppose an error occurs here var myObjectTwo = new MyObjectTwo() { Name = "Notebook", Price = 100, ReferenceId = myVal }; context.MyObjectTwos.Add(myObjectTwo); context.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); } }
}
thanks... :confused:
-
Hi, RollBack method; Roll back whatever changes have been made to the database. Following example I didn't use Commit(), what will RollBack() do? What exactly did transaction.Rollback() do?
using (var context = new AppDbContext())
{
using (var transaction = context.Database.BeginTransaction())
{
try
{
var myObjectOne = new MyObjectOne() { Name = "Book" };
context.MyObjectOnes.Add(myObjectOne);
context.SaveChanges();var myVal = myObjectOne.Id \* 3.14; //Suppose an error occurs here var myObjectTwo = new MyObjectTwo() { Name = "Notebook", Price = 100, ReferenceId = myVal }; context.MyObjectTwos.Add(myObjectTwo); context.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); } }
}
thanks... :confused:
-
Thanks for the answer @Richard-MacCutchan I read the article, but I didn't get the exact answer to my question. The EF Core working structure is different. Each process is primarily tracked on the memory. Makes my question a little clearer. What is the difference in effect between the two codes below? FIRST CODE:
using (var context = new AppDbContext())
{
using (var transaction = context.Database.BeginTransaction())
{
try
{
var myObjectOne = new MyObjectOne() { Name = "Book" };
context.MyObjectOnes.Add(myObjectOne);
context.SaveChanges();var myVal = myObjectOne.Id \* 3.14; //Suppose an error occurs here var myObjectTwo = new MyObjectTwo() { Name = "Notebook", Price = 100, ReferenceId = myVal }; context.MyObjectTwos.Add(myObjectTwo); context.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); } }
}
SECOND CODE:
using (var context = new AppDbContext())
{
using (var transaction = context.Database.BeginTransaction())
{
try
{
var myObjectOne = new MyObjectOne() { Name = "Book" };
context.MyObjectOnes.Add(myObjectOne);
context.SaveChanges();var myVal = myObjectOne.Id \* 3.14; //Suppose an error occurs here var myObjectTwo = new MyObjectTwo() { Name = "Notebook", Price = 100, ReferenceId = myVal }; context.MyObjectTwos.Add(myObjectTwo); context.SaveChanges(); transaction.Commit(); } catch (Exception ex) { //Nothing } }
}
-
Thanks for the answer @Richard-MacCutchan I read the article, but I didn't get the exact answer to my question. The EF Core working structure is different. Each process is primarily tracked on the memory. Makes my question a little clearer. What is the difference in effect between the two codes below? FIRST CODE:
using (var context = new AppDbContext())
{
using (var transaction = context.Database.BeginTransaction())
{
try
{
var myObjectOne = new MyObjectOne() { Name = "Book" };
context.MyObjectOnes.Add(myObjectOne);
context.SaveChanges();var myVal = myObjectOne.Id \* 3.14; //Suppose an error occurs here var myObjectTwo = new MyObjectTwo() { Name = "Notebook", Price = 100, ReferenceId = myVal }; context.MyObjectTwos.Add(myObjectTwo); context.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); } }
}
SECOND CODE:
using (var context = new AppDbContext())
{
using (var transaction = context.Database.BeginTransaction())
{
try
{
var myObjectOne = new MyObjectOne() { Name = "Book" };
context.MyObjectOnes.Add(myObjectOne);
context.SaveChanges();var myVal = myObjectOne.Id \* 3.14; //Suppose an error occurs here var myObjectTwo = new MyObjectTwo() { Name = "Notebook", Price = 100, ReferenceId = myVal }; context.MyObjectTwos.Add(myObjectTwo); context.SaveChanges(); transaction.Commit(); } catch (Exception ex) { //Nothing } }
}
Probably not much, but pay attention to the documentation:
DbTransaction.Dispose Method (System.Data.Common) | Microsoft Learn[^]:
Dispose
should rollback the transaction. However, the behavior ofDispose
is provider specific, and should not replace callingRollback
.In other words, the
using
block should roll-back the transaction if you haven't calledCommit
; but different database providers might not implement that correctly, and might require you to explicitly callRollback
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Probably not much, but pay attention to the documentation:
DbTransaction.Dispose Method (System.Data.Common) | Microsoft Learn[^]:
Dispose
should rollback the transaction. However, the behavior ofDispose
is provider specific, and should not replace callingRollback
.In other words, the
using
block should roll-back the transaction if you haven't calledCommit
; but different database providers might not implement that correctly, and might require you to explicitly callRollback
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thank you for the explanation. Actually, I guessed but wasn't sure. Many thanks @RichardDeeming @Richard-MacCutchan