null
null

Reputation: 807

Issue with InMemory DB Entity Framework Core 6 - method not found

I am trying to migrate a project from .NET Core 3.1 to .NET 6, and I am getting this error for the test project

System.MissingMethodException: Method not found: 'Void Microsoft.EntityFrameworkCore.MutableAnnotatableExtensions.SetOrRemoveAnnotation(Microsoft.EntityFrameworkCore.Metadata.IMutableAnnotatable, System.String, System.Object)'.

Stack Trace: 

RelationalEntityTypeExtensions.SetTableName(IMutableEntityType entityType, String name)
RelationalEntityTypeBuilderExtensions.ToTable(EntityTypeBuilder entityTypeBuilder, String name)
RelationalEntityTypeBuilderExtensions.ToTable[TEntity](EntityTypeBuilder1 entityTypeBuilder, String name) <>c.<OnModelCreating>b__359_0(EntityTypeBuilder1 entity) ModelBuilder.Entity[TEntity](Action1 buildAction) EntitiesContext.OnModelCreating(ModelBuilder modelBuilder) ModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context) ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, ModelDependencies modelDependencies) ModelSource.GetModel(DbContext context, ModelCreationDependencies modelCreationDependencies, Boolean designTime) DbContextServices.CreateModel(Boolean designTime) DbContextServices.get_Model() <>c.<TryAddCoreServices>b__8_4(IServiceProvider p) CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context) CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) <>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope) ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) ServiceProviderEngineScope.GetService(Type serviceType) ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) DbContext.get_DbContextDependencies() DbContext.get_ContextServices() DbContext.get_InternalServiceProvider() IServiceProvider>.get_Instance() InfrastructureExtensions.GetService[TService](IInfrastructure1 accessor) AccessorExtensions.GetService[TService](IInfrastructure`1 accessor) DatabaseFacade.get_Dependencies() DatabaseFacade.EnsureCreated() DbContextTestBase.ctor() line 27 MessageProcessorFixture.ctor() line 29 RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)

This is my InMemory db builder

public class DbContextTestBase : IDisposable
{
    private bool isDisposed = false;
    private readonly LoggerFactory _loggerFactory = new LoggerFactory(new[] { new DebugLoggerProvider() });
    protected EntitiesContext DbContext { get; }

    public DbContextTestBase()
    {
        var options = new DbContextOptionsBuilder<EntitiesContext>()
            .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
            .EnableSensitiveDataLogging(true)
            .UseLoggerFactory(_loggerFactory)
            .UseLazyLoadingProxies()
            .ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning))
            .Options;

        DbContext = new EntitiesContext(options);
        DbContext.Database.EnsureCreated();
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!isDisposed)
        {
            if (disposing)
            {
                DbContext.Database.EnsureDeleted();
                DbContext.Dispose();
            }

            isDisposed = true;
        }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

It happens on this line

DbContext.Database.EnsureCreated();

Any help will be appreciated

Upvotes: 4

Views: 6859

Answers (4)

Edgaras
Edgaras

Reputation: 527

I would like to expand on the accepted answer since I had the same error. This happens when you are mixing your entity framework library versions.

For example: If you have a code base that uses Entity Framework 5.0.10 and you install Entity Framework 6.0.0 (like Microsoft.EntityFrameworkCore.InMemory) package in your parent project (like tests project) then you will get this exception.

Instead, make sure to match up your versions, in this case, the test project would need Microsoft.EntityFrameworkCore.InMemory 5.0.10 too, or the 5.0.10 upgraded to 6.0.0.

Upvotes: 3

Tao G&#243;mez Gil
Tao G&#243;mez Gil

Reputation: 2757

In my case, I did not have a direct dependency towards the Microsoft.EntityFrameworkCore.Relational package, but a transitive one.

To find if that's your case, you can use this method: https://stackoverflow.com/a/62225395/2651069

And if that's the case, you can fix the problem by adding an explicit reference to the 6.x version in your *.csproj file.

Upvotes: 1

awroggeband
awroggeband

Reputation: 1

This can also happen if you are using FluentAPI commands that are incompatible with the target database:

e.g.

            modelBuilder.Entity<Control>(entity =>
            {
                entity.HasKey(e => e.ID)
                    .HasName("PK_Control")
                    .IsClustered(false);
            ...

The DbContext was scaffolded from SQL Server. The crash happened on the IsClustered(false) line, apparently not supported on either the EF In-memory database or SQLite.

The solution for unit testing was to create an interface from the original DbContext and use the interface in the Method to be tested. For the unit tests, create a DbContext for testing with the problematic code removed, based on the same interface.

Upvotes: 0

null
null

Reputation: 807

The solution was upgrading my entities package to .NET 6

Upvotes: 6

Related Questions