MissingMethodException in Integration test with sqlite

I have the following exception:
enter image description here

I was using .NET 6 (I still had the error) and thought that it could be some conflict with the nugets I am using in my solution. It turns out that even after updating to .NET 7 the error persists when I run the test. For testing I’m using MSTest Framework and using an inmemory database (sqlite) to make integration tests. The error is happening when executing the line await context.Database.EnsureCreatedAsync();. The test classes are the following:

public class SQLiteDatabaseContextFactory : IDisposable
{
    private DbConnection _connection;

    private DbContextOptions<DataContext> CreateOptions()
    {
        return new DbContextOptionsBuilder<DataContext>()
            .UseSqlite(_connection).Options;
    }

    public DataContext CreateContext()
    {
        if (_connection == null)
        {
            _connection = new SqliteConnection("DataSource=:memory:");
            _connection.Open();

            var options = CreateOptions();
            using var context = new DataContext(options);
            context.Database.EnsureCreated();
        }

        return new DataContext(CreateOptions());
    }

    public void Dispose()
    {
        if (_connection != null)
        {
            _connection.Dispose();
            _connection = null;
        }
    }
}

And:

[TestClass]
public class SQLiteIntegrationTests
{
    [TestMethod]
    public async Task TestMethod_UsingSqliteInMemoryProvider_Success()
    {
        using var connection = new SqliteConnection("DataSource=:memory:");
        connection.Open();

        var options = new DbContextOptionsBuilder<DataContext>()
            .UseSqlite(connection) // Set the connection explicitly, so it won't be closed automatically by EF
            .Options;

        // Create the dabase schema
        // You can use MigrateAsync if you use Migrations
        using (var context = new DataContext(options))
        {
            //await context.Database.MigrateAsync();
            await context.Database.EnsureCreatedAsync();
        } // The connection is not closed, so the database still exists

        using (var context = new DataContext(options))
        {
            var user = new ManualClassifier()
            {
                FirstName = "First",
                LastName = "Last",
                Email = "example@gmail.com",
                Username = "firstlast123",
                PasswordHash = "5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf",
                PasswordSalt = "5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf"

            };
            context.ManualClassifiers.Add(user);
            await context.SaveChangesAsync();
        }

        using (var context = new DataContext(options))
        {
            var count = await context.ManualClassifiers.CountAsync();
            Assert.AreEqual(1, count);

            var u = await context.ManualClassifiers.FirstOrDefaultAsync(user => user.Email == "example@gmail.com");
            Assert.IsNotNull(u);
        }
    }
}

EDIT: The full error is the following:
enter image description here

The .csproj of the project where I’m running the tests:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.13" />
    <PackageReference Include="MSTest.TestFramework" Version="3.0.2" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\VSC.Repo\VSC.Repo.csproj" />
  </ItemGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
        <PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
        <PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
        <PackageReference Include="coverlet.collector" Version="1.3.0" />
    </ItemGroup>

</Project>

Dbcontext class library .csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
    <ItemGroup>
        <FrameworkReference Include="Microsoft.AspNetCore.App" />
    </ItemGroup>
    <ItemGroup>
      <Folder Include="Services\" />
    </ItemGroup>
    <ItemGroup>
      <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
      <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.2">
        <PrivateAssets>all</PrivateAssets>
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      </PackageReference>
      <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2" />
      <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.2">
        <PrivateAssets>all</PrivateAssets>
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      </PackageReference>
      <PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0" />
    </ItemGroup>

</Project>

Any help figuring what is happening would be much appreciated. I honestly have no clue what is causing this.

>Solution :

Looks a lot like package version mismatch (in my practice it is the most common source of such errors). Update Microsoft.EntityFrameworkCore.Sqlite to the latest 7th version (my guess is that EF project uses that version) to match major version of EF Core packages in the tested solution.

Leave a Reply