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>