【问题标题】:Entity Framework Core adding to .Net Core Web Api - IRelationalTypeMappingSource problemEntity Framework Core 添加到 .Net Core Web Api - IRelationalTypeMappingSource 问题
【发布时间】:2020-12-14 02:07:57
【问题描述】:

我的 .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>

【问题讨论】:

  • 能否提供一些代码
  • App 不能运行任何代码,从这里开始会出现异常: public myDbContext(DbContextOptions options) : base(options) { }
  • 使用您尝试运行的代码编辑问题,如果不知道问题出在哪里,很难提供任何帮助。
  • 可以添加csproj吗?
  • 我添加了.csproj

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      如果您希望 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" />
      

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

      【讨论】:

        猜你喜欢
        • 2020-09-06
        • 1970-01-01
        • 2021-03-16
        • 2021-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-24
        • 2020-12-29
        相关资源
        最近更新 更多