I am using the dotMemoryUnit to prove the my DbContext object is getting garbage collected properly.
I feel that this code should work properly in a unit test, but the test always fails. The only thing I can guess is EF Core is holding a reference somewhere.
Edit: I'm not sure the suggested question addresses this problem as this is .Net Core and not .Net Framework. The documentation for GC.Collect()'s default is forced and the documentation says nothing about hints.
Edit 2: I did find the answer below.
Thoughts?
public class UnitTest1
{
public UnitTest1(ITestOutputHelper output)
{
DotMemoryUnitTestOutput.SetOutputMethod(output.WriteLine);
}
[Fact]
[DotMemoryUnit(FailIfRunWithoutSupport = false)]
public void DummyContext_DisposesContextOnGarbageCollect()
{
// Arrange
var options = new DbContextOptionsBuilder<DummyContext>()
.UseSqlServer("data source=ASqlServer;Integrated Security=true");
using (var ctx = new DummyContext(options.Options))
{
// do nothing
}
GC.Collect();
GC.WaitForPendingFinalizers();
dotMemory.Check(
memory =>
Assert.Equal(
0,
memory.GetObjects(where => where.Type.Is<DummyContext>()).ObjectsCount));
}
private class DummyContext : DbContext
{
public DummyContext(DbContextOptions options)
: base(options)
{
}
}
}