DEV Community

Julia Shevchenko
Julia Shevchenko

Posted on

2

Adding another database provider (for testing) is not working in EF Core 9

For writing integration tests, a common approach is to change the database provider, for example, to the InMemory database. But with Ef Core 9 this approach will give you an exception: "Only a single database provider can be registered in a service provider". So, what to do if there is a need to change the database provider and even manually removing another provider doesn't help?

Image description

Searching for possible solutions, I've found this issue on the ef core repository.
It turned out that a new IDbContextOptionsConfiguration interface was introduced, which, according to the documentation, configures the options to be used by a DbContext. And if it tracks multiple database providers, the exception will be thrown.

A workaround that I found in this article is to remove the configuration from the service provider. This solution works like a charm but feels a little bit hacky. Should we manually change the configuration? However, this may still save hours for those who faced it after updating the ef core.

The current version of the documentation here is not updated yet. However, the issue is in progress, and I'm curious what the suggested solution will be.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (1)

Collapse
 
chami profile image
Mohammed Chami • Edited

thank you for sharing

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay