title: Best way to coordinate atomic transactions between different business services in .NET Core / EF Core 2.1?
body: <p>Let's say you have a UsersService, a TenantsService, and a TenantUserRolesService. </p>
  
  <p>Each of these services has a "create" method to create their entity and has the same DbContext dependency injected, except for UsersService which has the Identity UserManager class injected.</p>
  
  <p>Your goal is to provide an API endpoint that lets a new tenant register which creates a User, a Tenant, and a TenantUserRole entity. TenantUserRole is created last and is an associative entity with foreign keys to the newly created User, the newly created Tenant, and the "Owner" Role.</p>
  
  <p>What's the best way to create an atomic transaction between these 3 services, so that if a single create fails, everything is rolled back and the DB is not in a broken state where there are tenants with no users or users with no tenants, etc.</p>
  
  <p>I have looked at: <a href="https://docs.microsoft.com/en-us/ef/core/saving/transactions" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/ef/core/saving/transactions</a>, but I can't figure out if there is a way to use those methods between different service classes. </p>
  
  <p>The only way I can think of is to take an imperative approach and manually check the result of each create, and if there is an error then use the same entity service to delete the recently created entity. </p>
  
  <p>I'm hoping there is someone who is a bit more knowledgeable on the subject that can point me in the right direction. </p>
  
  <p>Thanks</p>