9

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.

8 Answers 8

15

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

5
  • 3
    Thanks. Also, Oracle has dropped the ball so many times in the past so I will move to Pomelo. Oracle is the unique vendor that gives me so many troubles with .net (oci drivers, MySQL drivers, etc.)
    – magallanes
    Mar 19, 2022 at 12:05
  • 1
    As of May 18, 2022 this error is still present, so this seems to be the only reliable solution. May 18, 2022 at 13:58
  • Same error in net6.0.11 but this has saved me time, now i realize why it has lots of download compared to MySQL version in NuGet.
    – muhdamean
    Dec 6, 2022 at 9:14
  • This isn't really a fix, this is just using something different.
    – Dan Chase
    Dec 24, 2022 at 21:11
  • Also use Polemo to reverse engineering as create dbcontext with scaffold. Like this dotnet ef dbcontext scaffold "Server=localhost;Port=3306;Database=dbentity1;Uid=xxx;Pwd=xxxx;" Pomelo.EntityFrameworkCore.MySql -o Models -c "MyDbContext" Mar 18 at 6:57
9

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 :)

1
  • 2
    This is the best answer. Downgrading is the worst answer because Microsoft is being really picky about letting you even install .NET 5 any more. I tried to install it to linux and it was not in the repositories any more, had to install manually.
    – Dan Chase
    Sep 26, 2022 at 18:48
6

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.

2
  • 1
    how did you downgrade? added packages specifying lower version?
    – quinestor
    Jan 13, 2022 at 17:13
  • 2
    No need to downgrade. Look at the answer of MDave below, it looks like the MySql provider is buggy. The Pomelo provider does work and seems to be the more popular choice when looking at the download count at nuget.org.
    – metatron
    Mar 18, 2022 at 15:35
6

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.

2

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"

0

@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

0

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.

0

The latest version of MySql.EntityFrameworkCore still uses the EFCore 5.0 libraries and has not been updated to EFCore 6.0. It also appears that Microsoft.EntityFrameworkCore.Diagnostics was removed in 6.

I resolve this error by adding the class below to my code:

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

Then you must add it as a service in the initialization file

services.AddSingleton<IDesignTimeServices, MysqlEntityFrameworkDesignTimeServices>();

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.