Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with scaffolding a MySql database with EF Core - Method not found: Void Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping

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.Nullable1<System.Data.DbType>, Boolean, System.Nullable1)'.

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.

like image 345
Schalk van der Merwe Avatar asked Dec 07 '20 18:12

Schalk van der Merwe


People also ask

Does EF core work with MySQL?

MySql is the most popular Entity Framework Core provider for MySQL compatible databases. It supports EF Core 3.1 (and lower) and uses MySqlConnector for high-performance database server communication. The following versions of MySqlConnector, EF Core, . NET Standard and .

Is scaffold-DbContext command is used in EF Code First approach?

The EF core only supports Code First & Database First approach. In Database First, We use the Scaffold-dbcontext to create the Model from an existing database. This is basically Reverse engineering the existing database. Once we create the entity classes databases first does not work.


1 Answers

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.

like image 148
Bradley Grainger Avatar answered Oct 22 '22 03:10

Bradley Grainger