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

Viewed 7612

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.

7 Answers

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

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.

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.

According this description https://bugs.mysql.com/bug.php?id=106592

you can solve this error by adding below class to your code

public class MysqlEntityFrameworkDesignTimeServices : IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
    {
        serviceCollection.AddEntityFrameworkMySQL();
        new EntityFrameworkRelationalDesignServicesBuilder(serviceCollection)
            .TryAddCoreServices();
    }
}

and after define you have to add it as service in startup file like this

services.AddSingleton<IDesignTimeServices, MysqlEntityFrameworkDesignTimeServices>();

After these steps you can migrate your code :)

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"

@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

You do not need to perminately downgrade your EF version to scaffold, you can edit the project file to use

  <ItemGroup>
      <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.8">
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
          <PrivateAssets>all</PrivateAssets>
      </PackageReference>
      <PackageReference Include="MySql.EntityFrameworkCore" Version="5.0.8" />
  </ItemGroup>

Then you can run

dotnet ef dbcontext scaffold "[DSN String]"  MySql.EntityFrameworkCore -o [DBName]

Then change your project file back (or revert changes) to use the latest version of EF.

Dont forget to edit the "protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)" and remove the DSN it adds to the code.

Related