9
votes

I have .Net Core web API works fine. When I try to add Entity Framework Core, firstly, compile-error. It said, I must add Microsoft.Bcl.AsyncInterfaces even though I used .Net Core 3.1. When I added this, compiled well but when api run, it gives this exception. I cannot find any solution of this exception on internet:

When run .net core web api (debugging):

namespace CoreWebApi{
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();           

        services.AddDbContext<PKDbContext>(options =>
            options.UseMySql(Configuration.GetConnectionString("LocalConnection")));


        services.AddScoped(typeof(IBankProduct), typeof(BankProductRepo));
        services.AddScoped(typeof(IProductType), typeof(ProductTypeRepo));
        services.AddScoped(typeof(IProductRequest), typeof(ProductRequestRepo));
        services.AddScoped(typeof(IProfile), typeof(ProfileRepo));
        services.AddScoped(typeof(INotification), typeof(NotificationRepo));

    }

  
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }


        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}
}

Startup codes works fine. When my dbContext class (PKDbContext) runs, it gives exception on this part: (: base(options))

public class PKDbContext : DbContext{
    public PKDbContext() { }

    public PKDbContext(DbContextOptions<PKDbContext> options) : base(options)
    {
        // some codes
    }
}

Exception thrown: 'System.InvalidOperationException' in Microsoft.EntityFrameworkCore.dll An exception of type 'System.InvalidOperationException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code The database provider attempted to register an implementation of the 'IRelationalTypeMappingSource' service. This is not a service defined by EF and as such must be registered as a provider-specific service using the 'TryAddProviderSpecificServices' method.

*Edit: I am using Pomelo.EntityFrameworkCore.MySql

**Edit: I added csproj file code:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <StartupObject>CoreWebApi.Program</StartupObject>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0-preview.7.20365.15" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.0-preview.7.20365.15" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.2" />
  </ItemGroup>


</Project>
3
Could you please provide some code - SirOneOfMany
App can not run any code, it gives exception when starts at here: public myDbContext(DbContextOptions<myDbContext> options) : base(options) { } - compazizo
Edit the question with the code which you are trying to run, it's hard to provide any help without knowing where the issue lies. - Don Andre
Can you add the csproj? - vernou
I added .csproj - compazizo

3 Answers

7
votes

Had the same issue, turns out the current version of Pomelo.EntityFrameworkCore.MySql requires a version of Microsoft.EntityFrameworkCore between 3.1.8 and before 5.0.0

Downgrading Microsoft.EntityFrameworkCore to the version before 5.0.0 (3.1.11) solved the problem for me

3
votes

I decided to change db to Sql Server. I've removed Pomelo.EntityFrameworkCore.MySql and added Microsoft.EntityFrameworkCore.SqlServer and problem's solved.

0
votes

if you want EF Core 5 to work with MySQL you need to install Pomelo.EntityFrameworkCore.MySql 5.0.0-alpha.2

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.3" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.0-alpha.2" />

or you can downgrade Pomelo.EntityFrameworkCore.MySql to a more stable version which is 3.2.4 along with EF Core 3.1.12

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.12" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.2.4" />

PS: for more information visit Pomelo.EntityFrameworkCore.MySql compatibility section