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}
MySQL Installer or the MySQL Connector/NET MSI file. Install MySQL Connector/NET and then add a reference for the MySql.Data.EntityFramework assembly to your project. Depending on the .NET Framework version used, the assembly is taken from the v4.0, v4.5, or v4.8 folder. MySQL Connector/NET source code.
Setting the version for Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.Design to 3.1.10 and doing a rebuild, can be used as a work around. (For those that encountered this on first attempt in playing with .net like myself) This is the solution until the MySQL EF folks update their package.
.NET Framework 4.5.1 (.NET Framework 4.5.2 for Connector/NET 8.0.22) .NET Standard 2.1 (.NET Core SDK 3.1 and Visual Studio 2019 version 16.5) The MySQL Connector/NET 8.0 release series has a naming scheme for EF6 assemblies and NuGet packages that differs from the scheme used with previous release series, such as 6.9 and 6.10.
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
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With