title: XUnit Test DbContext did not dispose
body: <p>I am implementing a XUnit test for DbContext, it seems the DbContext did not dispose correctly. When I debug the first test, it works, but on the second test, an error <code>listAds</code> already added is thrown.</p>
  
  <p>After adding a watch, in the second test, <code>_context</code> is null, but it has values for <code>Advertisements</code> before <code>_context.Advertisements.AddRange(listAds);</code> is called.</p>
  
  <pre><code>public class AdsServiceTest: IDisposable
  {
      private readonly DbContextOptions&lt;SensingSiteDbContext&gt; _options;
      private readonly SensingSiteDbContext _context;
      private readonly AdsService _AdsService;
  
      public AdsServiceTest()
      {
          //initialize db options
          _options = new DbContextOptionsBuilder&lt;SensingSiteDbContext&gt;()
              .UseInMemoryDatabase()
              .Options;
  
          //get service
          _context = new SensingSiteDbContext(_options);
  
          //initialize dbcontext
          List&lt;Ads&gt; listAds = new List&lt;Ads&gt;() {
              new Ads(){  Id=1,AdsName="Ads1", Deleted=false},
              new Ads(){  Id=2,AdsName="Ads2", Deleted=false},
              new Ads(){  Id=3,AdsName="Ads3", Deleted=false}
          };
  
          //In the second test method, it throw errors, listAds already exist in 
          _context.Advertisements.AddRange(listAds);
          _context.SaveChanges();
          BaseLib.SSDbContext&lt;Ads, AdsService&gt; ssDbContent = new BaseLib.SSDbContext&lt;Ads, AdsService&gt;(_context);
          _AdsService = ssDbContent.GetService((x, y) =&gt; new AdsService(x, y));
  
      }
  
      public void Dispose()
      {
          _context.Dispose();            
      }
  
      [Theory]
      [InlineData(1)]
      public void FindById(int id)
      {
          Ads adsResult = _AdsService.FindById(id);
          Ads adsTarget = _context.Advertisements.Find(id);
          Assert.Equal(adsResult.AdsName, adsTarget.AdsName);
          //Assert.True(adsTarget.Equals(adsResult));
      }
  
      [Fact]
      public void GetAll()
      {
          var adsResult = _AdsService.GetAll();
          var adsTarget = _context.Advertisements.ToList();
          Assert.Equal(adsResult.Count(),adsTarget.Count());
          //Did not work all the time
          //Assert.True(adsTarget.Equals(adsResult));
      }
  }
  </code></pre>