title: Xunit new in memory database DBContext Class has live data
body: <p>I am trying to unit test with the EF Core in-memory database like so:</p>
  <pre><code>namespace ContosoTests
  {
      public class TrendServiceTests
      {
         private static Microsoft.EntityFrameworkCore.DbContextOptions&lt;TestContext&gt; options = new 
         Microsoft.EntityFrameworkCore.DbContextOptionsBuilder&lt;TestContext&gt;()
                         .UseInMemoryDatabase(Guid.NewGuid().ToString())
                         .Options;
  
         private static TestContext _context = new TestContext(options);
  
         private readonly TrendService trendService = new TrendService(_context);
  
          private void SeedInMemoryDb()
          {
              if (!_context.TrendHistories.Any())
              {
                  _context.TrendHistories.Add(new TrendHistory { IsActive = true, Quarter = E_Quarter.Q1, 
                  TrendYear = 2020 });
              }
  
              if (!_context.Controls.Any())
              {
                  _context.Controls.Add(new Control { IsActive = true});
                  _context.Controls.Add(new Control { IsActive = true});
                  _context.Controls.Add(new Control { IsActive = false});
              }
  
              _context.SaveChanges();
          }
  }
  </code></pre>
  <p>My TestContext inherits from my live context class to avoid the error:<br>
  <em>Services for database providers 'Microsoft.EntityFrameworkCore.InMemory', 'Microsoft.EntityFrameworkCore.SqlServer' have been registered in the service provider. Only a single database provider can be registered in a service provider</em></p>
  <p>So my TestContext just looks like:<br></p>
  <pre><code>public class TestContext : ContosoContext
      {
          public TestContext(DbContextOptions&lt;TestContext&gt; options)
          {
              
          }
  
          public TestContext()
          {
  
          }
      }
  </code></pre>
  <p>When I use the new instance of TestContext (_context) in my test class, all the live data for ContosoContext is there.
  I was expecting it to be a new instance of the context class with no data so that I could test my DAL code with controlled data. But this is not the case.
  <br><br>I am fairly new to unit testing so any help is much appreciated.</p>
  <p><strong>Edit:</strong><br>
  Here are the relevant parts of my contoso context class</p>
  <pre><code>    public class ContosoContext: IdentityDbContext&lt;ContosoApplicationUser&gt;
      {
          public ContosoContext(DbContextOptions&lt;ContosoContext&gt; options) : base 
          (options)
          {
  
          }
  
          public ContosoContext()
          {
  
          }
  
          //all my DBSets here
  
          protected override void OnConfiguring(DbContextOptionsBuilder 
          optionsBuilder)
          {
  
              optionsBuilder.UseSqlServer(&quot;MyConnectionString&quot;);
  
          }
      }
  </code></pre>
  <p>So the issue is that my onconfiguring method handles the connection string directly??</p>