question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Option to InMemoryDB to ignore Relational-specific methods

See original GitHub issue

Problem

We are using the InMemoryDatabase to test all our business logic. Now we have this one place where we need to ExecuteSqlRaw (to enable identity insert).

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

I know this issue popped up in the past several times - I guess with migrations. I myself run into this issue several times over the years.

Solution

I think it would be nice to have an option for the InMemoryDatabase to just silently ignore Relational-specific method calls instead of throwing this exception. The InMemoryDatabase already ignores so many relational-specific things like foreign keys or schema checks (not null, max length, etc.) so it doesn’t seem totally inappropriate to have such an option. Default disabled.

The InMemoryDatabase is a great tool for testing and this would make it even more useable.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ajcvickerscommented, Aug 13, 2021

@matthiaslischka As @roji said, we discussed this as a team and we believe things like this are exactly why using the in-memory provider for testing is an anti-pattern. We don’t plan to change the in-memory provider in the way you suggest. Instead, take a look at the documentation that @roji pointed to and consider using a relational provider for your testing.

1reaction
rojicommented, Aug 12, 2021

Thanks, we’ll discuss this as a team and see what we think.

FWIW we do have an explicitly documented consensus that InMemory is not a good choice for testing EF Core applications (see the docs). Either you want to completely mock out your data layer and test your business logic - in which case a repository pattern is the right layer to mock (and avoid issues such as raw SQL) - or you want to exercise your data layer, in which case you should ideally be testing against your production database system. In our experience many people end up using InMemory because they think running tests on SQL Server or other production databases isn’t feasible, and that isn’t really true.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Relational-specific methods can only be used when the ...
I am running Database Migration when Database ProviderName is not ... The solution was to skip setting the auto-increment value when in ...
Read more >
Exception when trying to use InMemoryDatabase for unit ...
Let the InMemoryDb ignore the relational-db specific calls, they shouldn't really be part of testable business logic anyway... but the way ...
Read more >
Avoid In-Memory Databases for Tests
While I agree that EF's in-memory implementation isn't adequate, it's mainly because it doesn't enforce DRI like a real database would. That's ...
Read more >
Testing with the InMemory provider in Entity Framework
How to use the InMemory provider in Entity Framework Core to test your methods without hitting the database or using fakes and mocks....
Read more >
Relational-specific methods can only be used when the ...
InvalidOperationException: Relational-specific methods can only be used when the context is using a relational database provider. entity-frameworkin-memory- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found