Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 285
Taufik Shaikh Avatar asked May 23 '18 09:05

Taufik Shaikh


2 Answers

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.

like image 190
Felix Avatar answered Nov 19 '22 02:11

Felix


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();
}
like image 34
Hemant Sakta Avatar answered Nov 19 '22 02:11

Hemant Sakta