Last week I had the pleasure to work with a team that started using Entity Framework Core for the first time. They had a lot of experience using NHibernate, so the concept of an ORM was not new. But it was interesting to see which things are obvious when switching to EF Core and which are not.
The first time they contacted me they had the following code in place:
A DbContext
(I switched the code to a simpler example):
And a minimal configuration in the Program.cs
file:
When executing this code, it resulted in the following error message:
No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
I think this error message is quite helpful but the team was still confused on how to solve this.
We had to apply 2 changes:
1. Update our DbContext
class to expect an options object and pass it to the base class:
2. Update our configuration to configure the specific database provider we want to use:
More information
https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/