Taufik Shaikh
Taufik Shaikh

Reputation: 339

System.InvalidOperationException: Relational-specific methods can only be used when the context is using a relational database provider

System.InvalidOperationException:

Relational-specific methods can only be used when the context is using a relational database provider.

Getting the above mentioned error while using InMemoryDatabase for Test Case?

var msaContextOptions = new DbContextOptionsBuilder<MSA.DAL.MsaDbContext>()
           .UseInMemoryDatabase(databaseName: "Get results")
           .ConfigureWarnings(w => w.Ignore(InMemoryEventId.TransactionIgnoredWarning))
           .Options;

Upvotes: 31

Views: 31900

Answers (3)

Felix
Felix

Reputation: 10078

Comparing with provider string is brittle - what if Microsoft changes to Microsoft.EntityFramework as it moves away from Core!

I would recommend to use

if (!context.Database.IsInMemory())
{
    context.Database.Migrate();
}

or

if (context.Database.IsRelational())
{
    context.Database.Migrate();
}

we have EF-related code in a separate nuget package that doesn't include Microsoft.EntityFrameworkCore.InMemory, therefore first option doesn't work for us.

Upvotes: 37

jasie
jasie

Reputation: 2444

In our case, the solution of @hemant-sakta didn't work. The cause for the error was that we set the auto-increment value in one of our database tables on seeding (we seed during migration).

The solution was to skip setting the auto-increment value when in Isolated Mode:

private static void SetAutoIncrementValue(MyContext context)
{
    if (HostingEnvironment.IsEnvironment("Isolated")) return;
    
    // auto increment code
}

Upvotes: 0

Hemant Sakta
Hemant Sakta

Reputation: 685

As mentioned by other people I found skipping DBMigration is the best option for now. I am running Database Migration when Database ProviderName is not InMemory.

if (context.Database.ProviderName != "Microsoft.EntityFrameworkCore.InMemory")
{
    context.Database.Migrate();
}

Upvotes: 23

Related Questions