Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core adding to .Net Core Web Api - IRelationalTypeMappingSource problem

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>
like image 650
compazizo Avatar asked Feb 03 '23 14:02

compazizo


2 Answers

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

like image 68
Tom Avatar answered Feb 06 '23 05:02

Tom


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

like image 44
compazizo Avatar answered Feb 06 '23 05:02

compazizo