title: Using Entity Framework Core 3.1 with UseInMemoryDatabase option in ServiceProvider ( Scoped lifetime )
body: <p>I have migrated a web application project from .NET Core 2.1 to 3.1 (also EF Core from 2.1.1 to 3.1.0).</p>
  
  <p>After the migration, some unit tests are not working anymore, throwing duplicate keys db exception. </p>
  
  <p>I simulated the problem and realize that EF core with option <code>UseInMemoryDatabase</code> is behaving differently in 3.1, it does not clean up the old data.</p>
  
  <p>In the second test method, the <code>People</code> table already contains data added from the first test, which is not happening in 2.1</p>
  
  <p>Does anyone know how can I make in-memory database to be scoped to each unit test?</p>
  
  <p>Here is my testing code:</p>
  
  <p><strong>AppDbContext.cs</strong></p>
  
  <pre><code>using Microsoft.EntityFrameworkCore;
  using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.Text;
  
  namespace MyConsoleApp.Database
  {
      public class AppDbContext: DbContext
      {
          protected AppDbContext(DbContextOptions options) : base(options) { }
  
          public AppDbContext(DbContextOptions&lt;AppDbContext&gt; options) : this((DbContextOptions)options)
          {
          }
  
          public virtual DbSet&lt;Person&gt; Person { get; set; }
      }
  
      public class Person
      {
          [Key]
          public int Id { get; set; }
          public string Name { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>AppUnitTest.cs</strong></p>
  
  <pre><code>using Microsoft.EntityFrameworkCore;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.VisualStudio.TestTools.UnitTesting;
  using MyConsoleApp.Database;
  using System.Linq;
  
  namespace MyConsoleAppTest
  {
      [TestClass]
      public class AppUnitTest
      {
          public ServiceCollection Services { get; private set; }
          public ServiceProvider ServiceProvider { get; protected set; }
  
          [TestInitialize]
          public void Initialize()
          {
             Services = new ServiceCollection();
  
             Services.AddDbContext&lt;AppDbContext&gt;(opt =&gt; opt.UseInMemoryDatabase(databaseName: "InMemoryDb"), 
                 ServiceLifetime.Scoped, 
                 ServiceLifetime.Scoped);
  
              ServiceProvider = Services.BuildServiceProvider();
          }
  
          [TestMethod]
          public void TestMethod1()
          {
              using (var dbContext = ServiceProvider.GetService&lt;AppDbContext&gt;())
              {
                  dbContext.Person.Add(new Person { Id = 0, Name = "test1" });
                  dbContext.SaveChanges();
                  Assert.IsTrue(dbContext.Person.Count() == 1);
              }
          }
  
          [TestMethod]
          public void TestMethod2()
          {
              using (var dbContext = ServiceProvider.GetService&lt;AppDbContext&gt;())
              {
                  dbContext.Person.Add(new Person { Id = 0, Name = "test2" });
                  dbContext.SaveChanges();
                  Assert.IsTrue(dbContext.Person.Count() == 1);
              }
          }
  
          [TestCleanup]
          public virtual void Cleanup()
          {
              ServiceProvider.Dispose();
              ServiceProvider = null;
          }
      }
  }
  
  </code></pre>
  
  <p><strong>MyConsoleAppTest.csproj</strong></p>
  
  <pre><code>&lt;Project Sdk="Microsoft.NET.Sdk"&gt;
  
    &lt;PropertyGroup&gt;
      &lt;TargetFramework&gt;netcoreapp3.1&lt;/TargetFramework&gt;
      &lt;IsPackable&gt;false&lt;/IsPackable&gt;
    &lt;/PropertyGroup&gt;
  
    &lt;ItemGroup&gt;
      &lt;PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" /&gt;
      &lt;PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" /&gt;
      &lt;PackageReference Include="MSTest.TestAdapter" Version="2.0.0" /&gt;
      &lt;PackageReference Include="MSTest.TestFramework" Version="2.0.0" /&gt;
      &lt;PackageReference Include="coverlet.collector" Version="1.0.1" /&gt;
    &lt;/ItemGroup&gt;
  
    &lt;ItemGroup&gt;
      &lt;ProjectReference Include="..\MyConsoleApp\MyConsoleApp.csproj" /&gt;
    &lt;/ItemGroup&gt;
  
  &lt;/Project&gt;
  </code></pre>