title: How to dispose/rollback a BeginTransaction() with several SaveChanges()
body: <p>I have a 3 layer structure in my solution, where the two lower ones are the logic layer and the database layer. The database layer consists of all CRUD methods for each entity in my database. And in the logic layer we have CRUD services for each entity database CRUD method (EntityService.Add/Get/Update/Delete -&gt; EntityRepository.Add/Get/Update/Delete).</p>
  <p>So here I am trying to do a big save method in services that saves several entities. The method calls several <code>Add</code> methods in the logic layer for each entity, which in their turn calls the database <code>Add</code> method. I might add that all database CRUD methods ends with <code>SaveChanges()</code>. Of course I want the save method to be <code>Rollbacked</code> if anything goes wrong so our database don't get unconnected rows, so I have added <code>BeginTransaction()</code> to the save method.</p>
  <p>Maybe worth mentioning is that after every CRUD we check so that nothing has gone wrong, and if it has we do a return with the returncode and whatnot. These returns happens before <code>transaction.Commit()</code></p>
  <p>The save method is structured like this: (Logic Layer)</p>
  <pre><code>public void SaveMethod(...)
  {
   using (var context = _unitOfWork.EntityRepository.GetContext()) //Gets the dbContext
          {
              using (var transaction = context.Database.BeginTransaction())
              {
                  try
                  {
                      Service.Entity1.AddEntity1();
                      
                      Service.Entity2.AddEntity2();
  
                      //And so on...
  
                      transaction.Commit();
                   }
                   catch{
                      //Exception handling...
                   }
                }
             }
          }
  </code></pre>
  <p>The method that gets the context to the Save method transaction: (database layer)</p>
  <pre><code>     public AniPlanContext GetContext()
      {
          var dbBuilder = new DbContextOptionsBuilder&lt;MyContext&gt;();
          var dbConn = _configuration.GetConnectionString(&quot;dbContextConnectionstring&quot;); //Gets the db connection string
          var options = dbBuilder.UseSqlServer(dbConn).Options;
  
          return new AniPlanContext(options);
      }
  </code></pre>
  <p>How an AddEntity method looks like: (Logic layer)</p>
  <pre><code>public Entity AddClinic(...)
      {
          try
          {
              //Validation....
  
             Entity entity = _unitOfWork.EntityRepository.Add(
                  new Entity
              {
                  //Set the attributes...
              });
  
              return entity;
          }
          catch (Exception ex)
          {
              //Exception
          }
      }
  </code></pre>
  <p>How a database Add looks like: (Database layer)</p>
  <pre><code>public TEntity Add(TEntity entity)
      {
          if (entity == null)
          {
              throw new ArgumentNullException($&quot;{nameof(Add)} entity must not be null&quot;);
          }
  
          try
          {
              _context.Add(entity);
              _context.SaveChanges();
  
              return entity;
          }
          catch (Exception)
          {
              throw new Exception($&quot;{nameof(entity)} could not be saved&quot;);
          }
      }
  </code></pre>
  <p>Anyhow, my issue is that <code>BeginTransaction</code> Commits the things that has done their <code>AddEntity()</code> even if the save method crashes or fails in any way. Can you help me understand why this is and how I can fix this?</p>
  <p>I have tried with <code>TransactionScope</code> and it works but when reading several blogposts it sounds like <code>BeginTransaction()</code> is the save and more reliant way to go. Of what I have understood, both of these should be <code>disposed</code> and <code>rollbacked</code> if it does not go through <code>transaction.Commit</code> or <code>scope.Complete</code>, is that correct?</p>
  <p>So to summarize or clarify: I'd like to use <code>BeginTransaction()</code> to save several entities to the database but that also rollback / dispose the transaction if anything has gone wrong.</p>