XUnit Test DbContext did not dispose

c# entity-framework-core xunit


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 listAds already added is thrown.

After adding a watch, in the second test, _context is null, but it has values for Advertisements before _context.Advertisements.AddRange(listAds); is called.

public class AdsServiceTest: IDisposable
    private readonly DbContextOptions<SensingSiteDbContext> _options;
    private readonly SensingSiteDbContext _context;
    private readonly AdsService _AdsService;

    public AdsServiceTest()
        //initialize db options
        _options = new DbContextOptionsBuilder<SensingSiteDbContext>()

        //get service
        _context = new SensingSiteDbContext(_options);

        //initialize dbcontext
        List<Ads> listAds = new List<Ads>() {
            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 
        BaseLib.SSDbContext<Ads, AdsService> ssDbContent = new BaseLib.SSDbContext<Ads, AdsService>(_context);
        _AdsService = ssDbContent.GetService((x, y) => new AdsService(x, y));


    public void Dispose()

    public void FindById(int id)
        Ads adsResult = _AdsService.FindById(id);
        Ads adsTarget = _context.Advertisements.Find(id);
        Assert.Equal(adsResult.AdsName, adsTarget.AdsName);

    public void GetAll()
        var adsResult = _AdsService.GetAll();
        var adsTarget = _context.Advertisements.ToList();
        //Did not work all the time
6/19/2019 9:52:47 AM

Accepted Answer

Context is disposed, but not "in-memory database" itself.

That's "by design" to allow you to test scenarios where classes you testing are creating it's own instance of DBContext - you can't prepare data for them otherwise.

You have two possibilities:

  1. Create "different databases" for each test with databaseName with code like .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())

  2. Force database destroy/recreate with context.Database.EnsureDeleted() after creation (or in Dispose).

For your test class method 2 looks more suitable, but it's up to you.

Docs: https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/in-memory

4/14/2017 8:58:53 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow