System.TypeLoadException: Method 'Create' in type 'MySql.Data.EntityFrameworkCore.Query.Internal.MySQLSqlTranslatingExpressionVisitorFactory'
Solution 1
If you're using Microsoft.EntityFrameworkCore 5.0 downgrade it to Microsoft.EntityFrameworkCore 3.1.10 MySQL EF 8.0.22 is currently not compatiable with Microsoft.EntityFrameworkCore 5.0
I have a working Code until I upgraded to Microsoft.EntityFrameworkCore 5.0 then I got same error, Downgraded and it worked!
Hoping to post a Bug report on MySQL Bug Forum
Solution 2
I have resolved this issue following this:-
upgrade Microsoft.EntityFrameworkCore to version 5.0.1
upgrade Pomelo.EntityFrameworkCore.MySql to version 5.0.0-alpha.2
add the code blocks into ConfigureServices method of Startup.cs file
var connectionString = _configuration.GetConnectionString("userDB"); services.AddDbContext(options =>
options.UseMySql(connectionString,new MySqlServerVersion(new Version(10, 1, 40)), mySqlOptions => mySqlOptions .CharSetBehavior(CharSetBehavior.NeverAppend)));
To get mysql server version run the query to any of mysql server editor like MySql Workbench or HeidiSQL
SELECT VERSION()
Solution 3
While following an EF Core tutorial for MySql using Migrations and getting the error "Method 'Create' does not have an implementation" and lots of Googling, I Downgraded my csproj file from net5.0 to netcoreapp3.1:
<TargetFramework>netcoreapp3.1</TargetFramework>
And included the following Nuget packages using terminal dotnet commands inside the project directory:
dotnet add package Pomelo.EntityFrameworkCore.MySql --version 5.0.0-alpha.2
...
dotnet list package
Project 'XYZ' has the following package references
[netcoreapp3.1]:
Top-level Package Requested Resolved
> Microsoft.EntityFrameworkCore 5.0.1 5.0.1
> Microsoft.EntityFrameworkCore.Design 5.0.1 5.0.1
> Microsoft.EntityFrameworkCore.Tools 5.0.1 5.0.1
> Pomelo.EntityFrameworkCore.MySql 5.0.0-alpha.2 5.0.0-alpha.2
As example, one of the model files is Student.cs:
namespace EFCoreTutorial.Model
{
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
}
}
I created the db context file SchoolContext.cs with my personal connection string as:
using Microsoft.EntityFrameworkCore;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
namespace EFCoreTutorial.Data
{
public class SchoolContext : DbContext
{
public DbSet<Model.Student> Students { get; set; }
public DbSet<Model.Course> Courses { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connectionString = "server=localhost;port=3306;database=SchoolDB;user=root;password=******";
optionsBuilder.UseMySql(connectionString, new MySqlServerVersion(new System.Version(8, 0, 22)), mySqlOptions => mySqlOptions.CharSetBehavior(CharSetBehavior.NeverAppend));
}
}
}
then you can create the database and the empty tables defined in the Model with
dotnet ef migrations add CreateSchoolDB <- just a label
dotnet ef migrations list <- to verify it is pending
dotnet ef database update <- the database will be created/updated
dotnet ef migrations list <- to verify
finally verify in mysql:
mysql> show databases;
mysql> use SchoolDB;
mysql> show tables;
Succes! The Database and tables were completely created from the models and DB Context definition.
Pedro Lucas Maia Ramos
Updated on June 22, 2022Comments
-
Pedro Lucas Maia Ramos almost 2 years
I'm trying to add a new user to the database with the following code:
public async void SeedUsers(){ int count=0; if(count>0){ return; } else{ string email="[email protected]"; _context.Add(new User{LoginEmail=email}); await _context.SaveChangesAsync(); } }
But it keeps giving me the following error:
System.TypeLoadException: Method 'Create' in type 'MySql.Data.EntityFrameworkCore.Query.Internal.MySQLSqlTranslatingExpressionVisitorFactory' from >assembly 'MySql.Data.EntityFrameworkCore, Version=8.0.22.0, Culture=neutral, >PublicKeyToken=c5687fc88969c44d' does not have an implementation. at MySql.Data.EntityFrameworkCore.Extensions.MySQLServiceCollectionExtensions.AddEntityFrameworkMySQL(IServ>iceCollection services) at MySql.Data.EntityFrameworkCore.Infrastructure.Internal.MySQLOptionsExtension.ApplyServices(IServiceColle>ction services) at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.ApplyServices(IDbContextOptions >options, ServiceCollection services) at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<c__DisplayClass4_0.g__BuildServiceProvider|3() at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_0.b__2(Int64 k) at System.Collections.Concurrent.ConcurrentDictionary
2.GetOrAdd(TKey key, Func
2 valueFactory) at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, >Boolean providerRequired) at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies() at Microsoft.EntityFrameworkCore.DbContext.EntryWithoutDetectChanges[TEntity](TEntity entity) at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState >entityState) at Microsoft.EntityFrameworkCore.DbContext.Add[TEntity](TEntity entity) at contatinApi.Data.SeedData.SeedUsers() in D:\dev\contatinapi\Data\SeedData.cs:line 24 at System.Threading.Tasks.Task.<>c.b__139_1(Object state) at System.Threading.QueueUserWorkItemCallback.<>c.<.cctor>b__6_0(QueueUserWorkItemCallback quwi) at System.Threading.ExecutionContext.RunForThreadPoolUnsafe[TState](ExecutionContext >executionContext, Action`1 callback, TState& state) at System.Threading.QueueUserWorkItemCallback.Execute() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()The User class:
public class User { public int Id{get;set;} public string LoginEmail{get;set;} public string UserName{get;set;} public DateTime CreatedAt{get;set;} public List<Contact> Contacts {get;set;} public List<ContactList> ContactLists{get;set;} }
Both the EF Core and MySQL packages are updated. Also, i tried using stored procedures and it gave the same results.
The content of Ex was:
The value of Ex was {System.TypeLoadException: Method 'Create' in type 'MySql.Data.EntityFrameworkCore.Query.Internal.MySQLSqlTranslatingExpressionVisitorFactory' from assembly 'MySql.Data.EntityFrameworkCore, Version=8.0.22.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' does not have an implementation. at MySql.Data.EntityFrameworkCore.Extensions.MySQLServiceCollectionExtensions.AddEntityFrameworkMySQL(IServiceCollection services) at MySql.Data.EntityFrameworkCore.Infrastructure.Internal.MySQLOptionsExtension.ApplyServices(IServiceCollection services) at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.ApplyServices(IDbContextOptions options, ServiceCollection services) at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_0.g__BuildServiceProvider|3() at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_0.b__2(Int64 k) at System.Collections.Concurrent.ConcurrentDictionary
2.GetOrAdd(TKey key, Func
2 valueFactory) at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, Boolean providerRequired) at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies() at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbContextDependencies.get_StateManager() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.EntryWithoutDetectChanges(TEntity entity) at Microsoft.EntityFrameworkCore.Internal.InternalDbSet
1.Add(TEntity entity) at contatinApi.Data.SeedData.SeedUsers() in D:\dev\ContatinApi\Data\SeedData.cs:line 40} -
Pedro Lucas Maia Ramos over 3 yearsThe Id field(key) is set to auto increment directly into the database and there's no required fields in the user class.
-
Serge over 3 yearsWhat about public DateTime CreatedAt{get;set;} . Can it be null? it should be public DateTime? CreatedAt{get;set;} or you have to assign date
-
Pedro Lucas Maia Ramos over 3 yearsIt's also added directly into the database. I added them into de object and still got an error.
-
Serge over 3 yearsWhat do you mean by "It's also added directly into the database. " and did you change _context.Add(newUser) to _context.Users.Add(newUser)?
-
Pedro Lucas Maia Ramos over 3 yearsI changed to _context.Users.Add and still got the error. In the database, the field createdAt has the default value of Now().
-
Serge over 3 yearsThis default values are very tricky. In order to trigger default value you have to post null value. DateTime by default has a very strange date so you have to change your class property to DateTime? or assign the real value before adding.
-
Pedro Lucas Maia Ramos over 3 yearsI tried using DateTime.Now on the object and still got the same error. Could it be the navigation properties(Contacts and ContactLists)?
-
Serge over 3 yearsI don't think so. I've used Add() for thousand times and it always worked. Did you try to get users? Maybe it's problem with _context?
-
Serge over 3 yearsI updated code. Pls run it in debuger and check var ex
-
Pedro Lucas Maia Ramos over 3 yearsUpdated the question, ex value was exactly the same error.
-
Pedro Lucas Maia Ramos over 3 yearsStill same error. Could it be the MySQL package not being compatible with the current version of EF Core?
-
Serge over 3 yearsI am sorry but I can't help you. I've never worked with MySql and will never recomend this DB to anybody. I prefer the professional applications not open source. EF core were created by Microsoft and for MS SQL mostly.
-
Serge over 3 yearsI noticed that your User class doesn't have any attributes? Can you post your DB context USER part ?
-
Mark G over 3 yearsThe Pomelo provider for MySQL has prerelease support for EF Core 5.0 if you'd like to try that.
-
Snipe3000 over 3 years@MarkG I'm using Pomelo.EntityFrameworkCore.MySql 5.0.0-alpha.2 build. The same problem is still there.
-
Mark G over 3 yearsMake sure you're using
UseMySql()
instead ofUseMySQL()
if you still have a reference toMySql.Data.EntityFrameworkCore
in your project file.