title: Writing to log table after transaction rollback in EF
body: <p>I need to write to the log table after transaction rollback in EF.
  The current code:</p>
  
  <pre><code>using (MyDataBase db = new DataBase())
  {
      using (var dbContextTransaction = db.Database.BeginTransaction())
      {
          try
          {
              //Inserting several records to different tables
              var newEntity = new Entity();
              ...
              db.Entity.Add(newEntity);
  
              db.SaveChanges();
              db.up_StoredProcCall;
              dbContextTransaction.Commit();
          }
          catch (Exception ex)
          {
              dbContextTransaction.Rollback();
  
              var logEntry = new LogEntry();
              ...
              db.LogEntry.Add(logEntry);
              db.SaveChanges();
          }
      }
  }
  </code></pre>
  
  <p>It does not work as EF caches all inserts internally and flushes them <strong>all</strong> along with a log record in a single transaction on:</p>
  
  <pre><code>db.LogEntry.Add(logEntry);
  db.SaveChanges();
  </code></pre>
  
  <p>What is the best way to achive the goal:</p>
  
  <ol>
  <li>Create a separate DbContext to insert a log record?</li>
  <li>Discard changes via the ChangeTracker like described <a href="https://stackoverflow.com/questions/16437083/dbcontext-discard-changes-without-disposing">here</a> or <a href="https://stackoverflow.com/questions/5466677/undo-changes-in-entity-framework-entities">here</a> or <a href="https://stackoverflow.com/questions/10535377/dbcontext-and-rejectchanges">here</a>? Personally I do not think it's a good idea as then I will not need to use transactions at all. </li>
  <li>Any other options are much appreciated</li>
  </ol>