Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. What does the Rollback method in EF Core do?

What does the Rollback method in EF Core do?

Scheduled Pinned Locked Moved .NET (Core and Framework)
asp-netdatabasehelptutorialquestion
5 Posts 3 Posters 9 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dataminers
    wrote on last edited by
    #1

    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:

    L 1 Reply Last reply
    0
    • D dataminers

      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:

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      See ROLLBACK TRANSACTION (Transact-SQL) - SQL Server | Microsoft Learn[^].

      D 1 Reply Last reply
      0
      • L Lost User

        See ROLLBACK TRANSACTION (Transact-SQL) - SQL Server | Microsoft Learn[^].

        D Offline
        D Offline
        dataminers
        wrote on last edited by
        #3

        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
            }
        }
        

        }

        Richard DeemingR 1 Reply Last reply
        0
        • D dataminers

          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
              }
          }
          

          }

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          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 of Dispose is provider specific, and should not replace calling Rollback.

          In other words, the using block should roll-back the transaction if you haven't called Commit; but different database providers might not implement that correctly, and might require you to explicitly call Rollback.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          D 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            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 of Dispose is provider specific, and should not replace calling Rollback.

            In other words, the using block should roll-back the transaction if you haven't called Commit; but different database providers might not implement that correctly, and might require you to explicitly call Rollback.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            D Offline
            D Offline
            dataminers
            wrote on last edited by
            #5

            Thank you for the explanation. Actually, I guessed but wasn't sure. Many thanks @RichardDeeming @Richard-MacCutchan

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups