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.
Yesterday I shared a first problem they encountered. Shortly after I explained a possible solution they contacted me back with a new error message:
System.ArgumentException : 'AddDbContext' was called with configuration, but the context type 'NorthwindDbContext' only declares a parameterless constructor. This means that the configuration passed to 'AddDbContext' will never be used. If configuration is passed to 'AddDbContext', then 'Northwind1DbContext' should declare a constructor that accepts a DbContextOptions<NorthwindDbContext> and must pass it to the base constructor for DbContext.
So what was the problem this time? They clearly didn’t were paying attention when I was explaining the solution.
When I took a look at the code I noticed the following:
They had applied the configuration change but forgot to update the DbContext to expect a parameter of type DbContextOptions
. I already shared the fix in the previous post, but here it is again: