5
votes

I am trying to connect my web API to MySql database with this code:

public class Startup
{       
    public void ConfigureServices(IServiceCollection services)
    {
        string conn_string = "server=server;database=database;uid=uid;pwd=pwd;";
        MySqlConnection conn = new MySqlConnection(conn_string);
        services.AddDbContext<TaskContext>(options =>
        {
            options.UseMySQL(conn);
        });
        services.AddMvc();
    }      
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseMvc();
    }
}

But I always recieve a System.TypeLoadException with this description:

System.TypeLoadException : 'Method 'Clone' in type 'MySQL.Data.EntityFrameworkCore.Infraestructure.Internal.MySQLOptionsExtension' from assembly 'MySql.Data.EntityFrameworkCore, Version=8.0.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' does not have an implementation.'

I am using Microsoft Visual Studio Community 2017 Preview (2) and my projet is in .NET Core 2.0. I also use MySql.Data.EntityFrameworkCore.dll and Microsoft.AspNetCore.All (2.0.0-preview2-final). I have changed many times of librairy for MySql but without success.

Any idea why this always happen? What could be the cause?

4

4 Answers

7
votes

Well, it turns out that you can't use MySql.Data.EntityFrameworkCore for .Net Core 2.0(for now). I had to go back to .Net Core 1.3 to make it work... We may be able to use it in a near futur tho!

6
votes

Use Pomelo.EntityFrameworkCore.MySql 2.0.0. Works fine for me with .NET Core 2.0

1
votes

Add "Pomelo.EntityFrameworkCore.MySql" package. in Stertup.cs & appsettings.json & DbContext :

services.AddDbContext<mvccoreContext>(options =>
         options.UseMySql(Configuration.GetConnectionString("DefaultConnection")
        ));


     {
      "ConnectionStrings": {
        "DefaultConnection": "Server=localhost;Database=mvccore;User=root;Password=;"
      },
      "Logging": {
        "LogLevel": {
          "Default": "Warning"
        }
      },
      "AllowedHosts": "*"
    }


protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseMySql("");
        }
    }
0
votes

MySql.Data.EntityFrameworkCore supports (and is only compatible with) EF Core 2.1. Using mismatched EF Core and DB provider libraries will result in errors similar to the one you reported.

If you need a MySQL EF Core provider that's compatible with EF Core 3.0, your only option right now is the 3.0.0-rc1 version of https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql (see the compatibility table at https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#compatibility)! ).

Otherwise, if you want to stick with MySql.Data.EntityFrameworkCore you'll need to roll back to EF Core 2.1.