'Unable to resolve service for type Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger

I am having difficulties to scaffold an existing MySQL database using EF core. I have added the required dependencies as mentioned in the oracle doc:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkcore.Tools" Version="6.0.0">

and then, I ran this code in the package manager console:

Scaffold-Dbcontext "server=the.server.ip.address;user=user_name;database=db_name;password=db_password;port=3306" MySql.EntityFrameworkCore -o Data -v

It shows this error:

Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger`1[Microsoft.EntityFrameworkCore.DbLoggerCategory+Scaffolding]' while attempting to activate 'MySql.EntityFrameworkCore.Scaffolding.Internal.MySQLDatabaseModelFactory'

Here are the relevant logs in the output window:

Finding design-time services referenced by assembly 'Test2'...
Finding design-time services referenced by assembly 'Test2'...
No referenced design-time services were found.
Finding design-time services for provider 'MySql.EntityFrameworkCore'...
Using design-time services from provider 'MySql.EntityFrameworkCore'.
Finding IDesignTimeServices implementations in assembly 'Test2'...
No design-time services were found.

I don't know how shall I implement the design time classes and nor did I find any useful links in the web.

Note that I can access and run query on the database using MySQL Workbench.



Solution 1:[1]

I came across the same issue trying to scaffold an existing MySQL database. It looks like the latest version of MySql.EntityFrameworkCore (6.0.0-preview3.1) still uses the EFCore 5.0 libraries and has not been updated to EFCore 6.0.

It also seems Microsoft.EntityFrameworkCore.Diagnostics was last implemented in EFCore 5 and removed in 6.

When I downgraded all the packages to the 5 version level, I was able to run the scaffold command without that error.

Solution 2:[2]

I got this error not while scaffolding but when trying to create my first migration. I didn't want to downgrade to 5.0 because it would've had to be permanent since I was going to run a lot of migrations.

I fixed it by changing my provider from MySql.Data.EntityFrameworkCore to Pomelo.EntityFrameworkCore.MySql

  1. Remove the old provider from both my API and DAL projects:

    dotnet remove package MySql.EntityFrameworkCore

  2. Add Pomelo provider

    dotnet add package Pomelo.EntityFrameworkCore.MySql

  3. Update your startup to use Pomelos configuartion:

    https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#2-services-configuration

  4. Migrations are working:

    dotnet ef migrations add InitialCreate

Solution 3:[3]

To make Scaffold-Dbcontext work, I had to downgrade both the packages to 5.0.0 version.

  • MySql.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.Tools

enter image description here

After successful scaffolding, I upgraded these packages back to the their latest versions.

Solution 4:[4]

I could find the Core 6.0 documentation for microsoft.entityframeworkcore.diagnostics in the official website, but it was still not working when I tried the Scaffold-DbContext command. Had to downgrade all the packages to the last 5.0 version before it worked. Here are the packagerefs in my project settings

"Microsoft.EntityFrameworkCore.Design" Version="5.0.13"

"Microsoft.EntityFrameworkCore.Tools" Version="5.0.13"

"MySql.Data" Version="8.0.28"

"MySql.EntityFrameworkCore" Version="5.0.10"

Solution 5:[5]

@https://stackoverflow.com/users/1322226/quinestor I faced same issue and as @https://stackoverflow.com/users/9914700/james-ruth mentioned I downgraded the versions of all EFCore and EFCore Design to 5.0.8 in Visual Studio.

Did not look around for command line commands :) But we can also do it from from dotnet cli, which I guess you probably would be aware of. We can remove - dotnet remove package <PACKAGE_NAME>, and install specific version - dotnet add package <PACKAGE_NAME> --version

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 James Ruth
Solution 2 MDave
Solution 3
Solution 4 suchintya
Solution 5 Raghavan MK