7

I'm trying to scaffold a MySql Database code first using MySql.Data.EntityFrameworkCore and Microsoft.EntityFrameworkCore on .NET Core 3.1 in Visual Studio 2019. However, I keep getting the following error:

Method not found: 'Void Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping..ctor(System.String, System.Type, System.Nullable`1<System.Data.DbType>, Boolean, System.Nullable`1<Int32>)'.

I looked at the source code for EF Core on GitHub and I can't find a method that matches that signature - am I missing something?

My scaffold code is:

dotnet ef dbcontext scaffold "Server=localhost;Database=database;Uid=myuid;" MySql.Data.EntityFrameworkCore -o Solution --project Solution.Project.Namespace

I also tried

dotnet ef dbcontext scaffold "Server=localhost;Database=database;Uid=myuid;" MySQL.Data.EntityFrameworkCore -o Solution --project Solution.Project.Namespace

And I tried running both of these commands via the Developer PowerShell and the Package Manager Console. Same result.

And my DbContext is

using Solution.Data.Models;
using Microsoft.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore; // I tried using MySql.Data.EntityFrameworkCore as well
using System;
using System.Collections.Generic;
using System.Text;

namespace Solution.Backend
{
    public class SolutionDBContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<Journal> Journals { get; set; }
        public DbSet<JournalComment> JournalComments { get; set; }
        public DbSet<CartItem> CartItems { get; set; }
        public DbSet<Order> Orders { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySQL("Server=localhost;Database=database;Uid=myuid;");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }
    }
}

My DbContext code is in a project called Solution.Backend and my models are in a project called Solution.Data

I get the same result if I comment out all the DbSets and try to run it without anything in the DbContext other than the OnConfiguring and OnModelCreating code. Am I missing something obvious or is there something I'm missing, or is this a known bug? I haven't found anything regarding this specific issue online anywhere. I'd appreciate some help here! Thanks in advance.

  • 1
    Make sure you are using the correct namespace. Namespaces are equivalent to Folder names and the classes are equivalent to filenames. VS when you have a unique class name it will automatically be found by the compiler. When you have duplicate class names you have to specify the full namespace and class name. – jdweng Dec 07 '20 at 18:41
  • Could you be more specific - which namespace is the issue? In ```dotnet ef dbcontext scaffold "Server=localhost;Database=database;Uid=myuid;" MySql.Data.EntityFrameworkCore -o *Solution* --project *Solution.Project.Namespace*``` I tried changing to -o *Solution*.*Project*.*Namespace* but it gave me the same issue. – Schalk van der Merwe Dec 07 '20 at 18:45
  • Change in code. I would look similar to following : Solution.Data.Solution.Backend.SolutionDBContext.Users – jdweng Dec 07 '20 at 21:37

1 Answers1

17

MySql.Data.EntityFrameworkCore 8.0.22 is only compatible with Microsoft.EntityFrameworkCore 3.1. Downgrade all Microsoft.EntityFramework packages from 5.0.0 to 3.1.10 to fix the error.

With the release of MySQL Connector/NET 8.0.23 on 18 Jan 2021, Oracle has now released a different NuGet package that's compatible with Microsoft.EntityFramework 5.0: MySql.EntityFrameworkCore.

See the table of version compatibility here.

Alternatively, you can try switching to Pomelo.EntityFrameworkCore.MySql 5.0.0-alpha.2 (or later); see its table of compatible package versions.

Bradley Grainger
  • 24,251
  • 4
  • 79
  • 101