title: Two DbContext's on same server throws: This platform does not support distributed transactions
body: <p>I am not able to figure out why TransactionScope is starting distributed transaction (which is not configured at the SQL Server). I would like to use local transaction instead, which can be used when two databases are located in the same SQL Server instance. What is wrong with my code, how can I fix it? Can I force Transaction Scope to try local transaction first?</p>
  
  <p><strong>Databases</strong></p>
  
  <p><a href="https://i.stack.imgur.com/MfiBQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MfiBQ.png" alt="enter image description here"></a></p>
  
  <p><strong>appsettings.json</strong></p>
  
  <pre><code>{
    "ConnectionStrings": {
      "DefaultConnection": "Data Source=DESKTOP;Initial Catalog=test;Integrated Security=True",
      "Test2Connection": "Data Source=DESKTOP;Initial Catalog=test2;Integrated Security=True"
    }
  }
  </code></pre>
  
  <p><strong>startup.cs</strong> registering TestContext and Test2Context</p>
  
  <pre><code>services.AddDbContext&lt;TestContext&gt;(options =&gt;
   options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
  
  services.AddDbContext&lt;Test2Context&gt;(options =&gt;
   options.UseSqlServer(Configuration.GetConnectionString("Test2Connection")));
  
  services.AddTransient&lt;ICustomerRepository, CustomerRepository&gt;();
  services.AddTransient&lt;IMaterialRepository, MaterialRepository&gt;();
  
  // This service inject TestContext and Test2Context
  services.AddTransient&lt;ICustomerService, CustomerService&gt;();
  
  services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  </code></pre>
  
  <p><strong>CustomerRepository</strong> using TestContext</p>
  
  <pre><code>public class CustomerRepository : ICustomerRepository
      {
          private readonly TestContext _context;
          public CustomerRepository(TestContext context)
          {
              _context = context;
          }
          public Customer Retrieve(int id)
          {
              return _context.Customers.Where(x =&gt; x.Id == id).FirstOrDefault();
          }
      }
  </code></pre>
  
  <p><strong>MaterialRepository</strong> using Test2Context</p>
  
  <pre><code>public class MaterialRepository : IMaterialRepository
      {
          private readonly Test2Context _context;
          public MaterialRepository(Test2Context context)
          {
              _context = context;
          }
          public Material Retrieve(int id)
          {
              return _context.Materials.Where(x =&gt; x.Id == id).FirstOrDefault();
          }
      }
  </code></pre>
  
  <p><strong>CustomerService</strong></p>
  
  <pre><code>public class CustomerService : ICustomerService
      {
          private readonly ICustomerRepository _customerRepository;
          private readonly IMaterialRepository _materialRepository;
          public CustomerService(
              ICustomerRepository customerRepository, 
              IMaterialRepository materialRepository)
          {
              _customerRepository = customerRepository;
              _materialRepository = materialRepository;
          }
          public void DoSomething()
          {
              using (var transaction = new TransactionScope(TransactionScopeOption.Required
                 //,new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead }
                  ))
              {
                  var customer = _customerRepository.Retrieve(1);
                  var material = _materialRepository.Retrieve(1); // The exception is thrown here !
                  // _customerRepository.Save(customer);
                  transaction.Complete();
              }
          }
      }
  </code></pre>
  
  <p>reading from second context throw's <code>This platform does not support distributed transactions</code> exception.</p>
  
  <p><a href="https://i.stack.imgur.com/9L44F.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9L44F.png" alt="enter image description here"></a></p>
  
  <p><strong>The distributed transaction is also firing, when using the same connection string for two database contexts</strong></p>
  
  <p><strong>startup.cs</strong></p>
  
  <pre><code>services.AddDbContext&lt;TestContext&gt;(options =&gt;
                   options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
  
  services.AddDbContext&lt;TestReadOnlyContext&gt;(options =&gt;  
                  options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
  </code></pre>
  
  <p><strong>CustomerReadOnlyRepository</strong></p>
  
  <pre><code>public class CustomerReadOnlyRepository : ICustomerReadOnlyRepository
      {
          private readonly TestReadOnlyContext _context;
          public CustomerReadOnlyRepository(TestReadOnlyContext context)
          {
              _context = context;
          }
          public Customer Retrieve(int customerId)
          {
              Customer customer = _context.Customers.Where(x =&gt; x.Id == customerId).Include("Offices").FirstOrDefault();
  
              return customer;
          }
      }
  </code></pre>
  
  <p><strong>CustomerService</strong></p>
  
  <pre><code>var customer = _customerRepository.Retrieve(1);
  var customerReadOnly = _customerReadOnlyRepository.Retrieve(1); // Throw's the same error.
  </code></pre>