title: Entity Framework Core Nested Transaction or update DbContext among saveChanges()
body: <p>I have this API which deletes and adds different users into the the Database. The way this API is written is something like this:</p>
  
  <pre><code>/* Assuming 'users' is an array of users to delete (if users[x].toDelete == true),
      or to add otherwise */
  using (var db = new myContext())
  {
      using (var dbTransaction = db.Database.BeginTransaction())
      {
          try
          {
              /* Performing deletion of Users in DB */
              SaveChanges();
  
              /* Performing add of Users in DB */
              foreach(user in users)
                  if (!user.toDelete) {
                      db.users.add(..);
                      db.SaveChanges();
                  }
  
              dbTransaction.Commit();
          }
          catch (ValidationException ex)
          {
              dbTransaction.Rollback();
          }
      }
  }
  </code></pre>
  
  <p>The reason why I use a transaction is that I have some validations that should be run on the new data.. For example, I cannot add two user with the same email!</p>
  
  <p>All the validation Server-Side is done with Data-Annotations and the MetaData classes, therefore I created the Attribute Uniqueness which I associated to the property email of the class UserMetaData.</p>
  
  <p>So, the problem is that, inside the Uniqueness Attribute I need to check again the database to search for other users with the same email:</p>
  
  <pre><code>public class IsUnique : ValidationAttribute
  {
      protected override ValidationResult IsValid(object value, ValidationContext validationContext)
      {
          ...
          using (var db = new myContext()) {
              /* Here I perform a select on the DB looking for records
                  with the same email, all using Reflection. */
              if (recordFound &gt; 0)
                  return new ValidationResult("email already exists");
  
              return ValidationResult.Success;
          }
      }
  }
  </code></pre>
  
  <p>So, as you can see, inside the validation I use <code>new myContext()</code> and that's where the problem is: let's pretend I have an empty database and I add two user with the same email in the same query.</p>
  
  <p>As you can see, I call the <code>db.SaveChanges()</code> after every user has been added, therefore I thought that, when the second user to be added would have been validated, my validation would have known that the first user added and the second one have the same email, therefore an Validation Exception would have been throw and the <code>dbTransaction.Rollback()</code> called.</p>
  
  <p>The problem is that inside the validator i call <code>new myContext()</code> and inside that context the changes that I thought would have been there thanks to the <code>db.SaveChanges()</code> there arent't, and that's I think is because all the changes are inside a Transaction.</p>
  
  <p>Sooo.. The solutions I thought so far are two:</p>
  
  <ul>
  <li>Nested Transaction: in the API I have an outer transaction which save the state of the DB at the beginning, then some inner transaction are run every time a <code>db.SaveChanges()</code> is called. Doing this, the validation does see the changes, but the problem is that, if in the catch i called <code>outerTransaction.rollback()</code>, the changes made by the inner transactions are not rollbacked;</li>
  <li>Some way to retrieve the context I have used in the API even in the validation.. But in there I only have these two arguments, so I don't know if it's even possible;</li>
  </ul>