title: Entity Framework - Issues in DbContextTransaction.Rollback()
body: <p>I connected the database using <strong><code>Entity Framework</code></strong>. I Implemented a Contact Repository to Save the Contact Information (Sample Repository). In the Said Class file, I declared a <code>static property</code> for handling <code>DBEntities</code> (i.e., <code>BLabsEntities</code>) namely "<code>Context</code>" and also declared a <code>static property</code> for <code>DbContextTransaction</code>.</p>
  
  <p>In the following code I added a try catch block to handle Exceptions of Entity Framework in the <code>SaveContact()</code> Repository method. I Initiated a <code>transaction</code> before starting the Save Contact process in the above said method, then I added a exceptional data into the database table <code>dbo.Contact</code> as per my thought it thrown the <code>exception</code>. In the Catch block I'm initiating the <code>Rollback</code> process of the said transaction and I'm trying to insert the Exception message in the <code>ErrorLog Table</code>, but the Context property holds the Contact information even-though the Rollback process executed. <strong>So, it throwing Exception in the catch block once again while on trying to insert the record in the <code>dbo.ErrorLog Table</code></strong>. I added a debugging snapshot for your reference.</p>
  
  <p>C# Code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Data.Entity;
  
  namespace EF_Sample
  {
      public class ContactRepository
      {
          private static BLabsEntities _context;
          private static DbContextTransaction _dbTransaction;
  
          protected static BLabsEntities Context
          {
              get { return _context ?? (_context = new BLabsEntities()); }
          }
  
          protected static DbContextTransaction DBTransaction
          {
              get { return _dbTransaction; }
              set { _dbTransaction = value; }
          }
  
          public void SaveContact(Contact contact = null)
          {
              try
              {
                  if (contact == null)
                  {
                      contact = new Contact()
                      {
                          FirstName = null,
                          LastName = null
                      };
                  }
  
  
                  BeginTransaction();
                  Context.Contacts.Add(contact);
                  Context.SaveChanges();
              }
              catch (Exception ex)
              {
                  CommitTransaction(false);
  
                  BeginTransaction();
                  ErrorLog error = new ErrorLog()
                  {
                      Message = ex.Message
                  };
  
                  Context.ErrorLogs.Add(error);
  
                  Context.SaveChanges();
              }
              finally
              {
                  CommitTransaction();
              }
          }
  
          private void BeginTransaction()
          {
              DBTransaction = Context.Database.BeginTransaction();
          }
  
          private void CommitTransaction(bool flag = true)
          {
              try
              {
                  if (flag)
                  {
                      DBTransaction.Commit();
                  }
                  else
                  {
                      DBTransaction.Rollback();
                  }
              }
              catch (Exception Ex)
              {
                  Console.WriteLine("Error: {0}", Ex.Message);
              }
          }
      }
  }
  </code></pre>
  
  <p>Debugging Snapshot:</p>
  
  <p><a href="https://i.stack.imgur.com/0ZwLE.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0ZwLE.jpg" alt="enter image description here"></a></p>
  
  <p>Exception in <code>try Block</code>: Explicitly Created Exception by Code</p>
  
  <p><a href="https://i.stack.imgur.com/BYvMR.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BYvMR.jpg" alt="enter image description here"></a></p>
  
  <p>Exception in <code>Catch Block</code>: Failure of <code>Rollback</code> operation</p>
  
  <p><a href="https://i.stack.imgur.com/h31cz.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/h31cz.jpg" alt="enter image description here"></a></p>
  
  <p>Database Table Structures:</p>
  
  <p><a href="https://i.stack.imgur.com/5Yzaa.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5Yzaa.jpg" alt="enter image description here"></a></p>
  
  <p>Kindly assist me how to do the <code>Rollback</code> operation successfully to <strong>clear the Context Changes</strong>.</p>