TypeLoadException Method 'IsValid' in type in Pomelo.EntityFrameworkCore.MySql.Update does not have an implementation

523 Views Asked by At

When I use Pomelo.EntityFrameworkCore.MySql in combination with Microsoft.AspNetCore.Identity I am getting that error.

Are my dependencies wrong ? Similar issue was reported 2021 already, but with older versions.
https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/1510

TypeLoadException: Method 'IsValid' in type 'Pomelo.EntityFrameworkCore.MySql.Update.Internal.MySqlModificationCommandBatch' from assembly 'Pomelo.EntityFrameworkCore.MySql, Version=6.0.1.0, Culture=neutral, PublicKeyToken=2cc498582444921b' does not have an implementation.

csproj

 <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
        <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.0-rc.2.21480.10" />
        <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.4" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0-preview.3.22175.1">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0-preview.3.22175.1">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="MySqlConnector" Version="2.1.8" />
        <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.1" />
    </ItemGroup>

ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {
    }
}

Programm.cs

var connectionString = builder.Configuration.GetConnectionString("Default");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
    options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
});

builder.Services.AddIdentity<IdentityUser, IdentityRole>(options =>
{
    options.Password.RequiredLength = 8;
    options.Password.RequireLowercase = true;
    options.Password.RequireUppercase = true;

    options.Lockout.MaxFailedAccessAttempts = 5;
    options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(15);
    options.User.RequireUniqueEmail = true;
}).AddEntityFrameworkStores<ApplicationDbContext>();

builder.Services.ConfigureApplicationCookie(options =>
{
    options.LoginPath = "/Account/Login";
    options.AccessDeniedPath = "/Account/AccessDenied";
});

var app = builder.Build();

Register

public class RegisterModel : PageModel
{
    private readonly UserManager<IdentityUser> userManager;

    public RegisterModel(UserManager<IdentityUser> userManager)
    {
        this.userManager = userManager;
    }

    [BindProperty] public RegisterViewModel RegisterViewModel { get; set; } 
..
..


  public async Task<IActionResult> OnPostAsync()
    {... 

 var result = await userManager.CreateAsync(user, RegisterViewModel.Password); // ERROR

}
2

There are 2 best solutions below

0
On BEST ANSWER

Try consolidating installed package versions especially preview ones (especially for EF 7). Only updated left:

<ItemGroup>
    ...
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.4">
        <PrivateAssets>all</PrivateAssets>
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.4">
        <PrivateAssets>all</PrivateAssets>
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    ...
</ItemGroup>
0
On

I was facing the same issue on VS2022 swagger. I managed to solve it by changing the versions of EF tools from 7.0.0 to 6.0.4 and Pomelo in version 6.0.1; Another alternative is to run "update-database" command in package manager console. I hope it helps!