Entity Framework Core添加到.Net Core Web Api - IRelationalTypeMappingSource问题

9

我有一个良好运行的.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
    }
}

抛出异常:'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>

2
请提供一些代码。 - SirOneOfMany
应用程序无法运行任何代码,它在此处启动时会出现异常: public myDbContext(DbContextOptions options) : base(options) { } - compazizo
请编辑您试图运行的代码问题,不知道问题所在很难提供任何帮助。 - Don Andre
你能添加这个csproj文件吗? - vernou
我添加了.csproj文件。 - compazizo
3个回答

7

我曾经遇到过同样的问题,后来发现Pomelo.EntityFrameworkCore.MySql当前版本需要使用Microsoft.EntityFrameworkCore 3.1.8至5.0.0之前的版本。

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


4

我决定将数据库更改为 Sql Server。我移除了 Pomelo.EntityFrameworkCore.MySql 并添加了 Microsoft.EntityFrameworkCore.SqlServer,问题得到解决。


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.4,并搭配EF Core 3.1.12

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

附注:欲了解更多信息,请访问Pomelo.EntityFrameworkCore.MySql兼容性部分


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接