Efcore: DbContext Models are not cached throughout xUnit test run

Created on 20 Mar 2018  路  3Comments  路  Source: dotnet/efcore

See https://github.com/aspnet/EntityFrameworkCore/issues/1906#issuecomment-374352462

Here are the test results that I got.
image

The CacheTestClassXXX classes use a simplified version of the work around described in https://github.com/aspnet/EntityFrameworkCore/issues/1906#issuecomment-374320307.

Notice that only CacheTestClass1 had the overhead of DbContext model creation.
But the TestClassXXX classes all had the overhead.

Steps to reproduce

  1. Create Class Library (.NET Framework 4.6.1) project.
  2. Add Dependencies (Nuget)... see below for list of dependencies
  3. Paste code below..
    ```C#
    using Microsoft.Data.Sqlite;
    using Microsoft.EntityFrameworkCore;
    using System;
    using Xunit;

public abstract class TestBase : IDisposable
{
private readonly SqliteConnection _connection;
protected readonly DbContextOptions _options;
public TestBase()
{
_connection = new SqliteConnection("DataSource=:memory:");
_connection.Open();
_options = new DbContextOptionsBuilder()
.UseSqlite(_connection)
.Options;
using (var context = new DbContext(_options))
{
context.Database.EnsureCreated();
}
}
public void Dispose()
{
_connection.Close();
}

[Fact]
public void DummyTestMethod() { }

}

public class TestClass1 : TestBase { }

public class TestClass2 : TestBase { }

public class TestClass3 : TestBase { }

[Collection(nameof(DbContextCache))]
public class CacheTestClass1 : TestBase { }

[Collection(nameof(DbContextCache))]
public class CacheTestClass2 : TestBase { }

[Collection(nameof(DbContextCache))]
public class CacheTestClass3 : TestBase { }

[CollectionDefinition(nameof(DbContextCache))]
public class DbContextCache : ICollectionFixture { }
```

Dependencies

microsoft.entityframeworkcore.sqlite v2.0.2
xunit v2.3.1
xunit.runner.visualstuio v2.3.1

Further technical details

.NET Framework 4.6.1
VS 2017 15.6.1
Windows 7

cc: @ajcvickers

closed-external

Most helpful comment

For more info on how to configure xunit not to use appdomains
https://xunit.github.io/docs/configuring-with-json

All 3 comments

@gojanpaolo I was able to reproduce this. The issue is that xunit (or the runner) is creating a new app domain for each test collection. This can be good for isolation, but does defeat EF caching. I believe that there are options to turn of this behavior in xunit, so that might be cleaner than what you have above. Also, when running on .NET Core there are no app domains, so xunit will never have this behavior there. (Unless they simulate it with multiple processes or something like that.) Closing as there is no EF actionable issue here.

For more info on how to configure xunit not to use appdomains
https://xunit.github.io/docs/configuring-with-json

@ajcvickers I see. Thank you for taking the time to look into this. :)
@smitpatel Awesome! Thanks for the link.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

0xdeafcafe picture 0xdeafcafe  路  189Comments

vsfeedback picture vsfeedback  路  98Comments

dragnilar picture dragnilar  路  236Comments

anpete picture anpete  路  100Comments

vijayantkatyal picture vijayantkatyal  路  321Comments