实体框架核心添加到.Net核心Web Api-IRelationalTypeMappingSource问题

时间:2020-08-25 11:30:57

标签: c# .net-core entity-framework-core asp.net-core-webapi

我有.Net Core Web API可以正常工作。当我尝试添加Entity Framework Core时,首先出现编译错误。它说,即使我使用.Net Core 3.1,也必须添加Microsoft.Bcl.AsyncInterfaces。当我添加此代码时,编译良好,但在api运行时,会出现此异常。我在互联网上找不到此异常的任何解决方案:

运行.net core Web API(调试)时:

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();
        });
    }
}
}

启动代码可以正常工作。当我的dbContext类(PKDbContext)运行时,它在此部分给出了例外:(:base(options)

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

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

引发的异常:Microsoft.EntityFrameworkCore.dll中的“ System.InvalidOperationException” Microsoft.EntityFrameworkCore.dll中发生类型'System.InvalidOperationException'的异常,但未在用户代码中处理 数据库提供程序尝试注册“ IRelationalTypeMappingSource”服务的实现。这不是EF定义的服务,因此必须使用“ TryAddProviderSpecificServices”方法注册为特定于提供商的服务。

*编辑:我正在使用Pomelo.EntityFrameworkCore.MySql

**编辑:我添加了csproj文件代码:

<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 个答案:

答案 0 :(得分:7)

有同样的问题,原来 Pomelo.EntityFrameworkCore.MySql 的当前版本需要 3.1.8 和 5.0.0 之前的 Microsoft.EntityFrameworkCore 版本

将 Microsoft.EntityFrameworkCore 降级到 5.0.0 (3.1.11) 之前的版本为我解决了问题

答案 1 :(得分:1)

我决定将db更改为Sql Server。我删除了Pomelo.EntityFrameworkCore.MySql并添加了Microsoft.EntityFrameworkCore.SqlServer,问题已解决。

答案 2 :(得分:0)

如果你想让 EF Core 5 与 MySQL 一起工作,你需要安装 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" />

或者您可以将 Pomelo.EntityFrameworkCore.MySql 降级到更稳定的版本,即 3.2.4EF Core 3.1.12

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

PS:欲了解更多信息,请访问 Pomelo.EntityFrameworkCore.MySql compatibility section