简体   繁体   English

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

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

I have .Net Core web API works fine.我有 .Net Core Web API 工作正常。 When I try to add Entity Framework Core, firstly, compile-error.当我尝试添加 Entity Framework Core 时,首先是编译错误。 It said, I must add Microsoft.Bcl.AsyncInterfaces even though I used .Net Core 3.1.它说,即使我使用了 .Net Core 3.1,我也必须添加 Microsoft.Bcl.AsyncInterfaces。 When I added this, compiled well but when api run, it gives this exception.当我添加这个时,编译得很好,但是当 api 运行时,它给出了这个异常。 I cannot find any solution of this exception on internet:我在互联网上找不到此异常的任何解决方案:

When run .net core web api (debugging):运行 .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();
        });
    }
}
}

Startup codes works fine.启动代码工作正常。 When my dbContext class (PKDbContext) runs, it gives exception on this part: ( : base(options) )当我的 dbContext 类(PKDbContext)运行时,它在这部分给出异常:( :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.引发的异常:Microsoft.EntityFrameworkCore.dll 中的“System.InvalidOperationException” Microsoft.EntityFrameworkCore.dll 中发生“System.InvalidOperationException”类型的异常,但未在用户代码中处理数据库提供程序尝试注册“IRelationalTypeMappingSource”的实现服务。 This is not a service defined by EF and as such must be registered as a provider-specific service using the 'TryAddProviderSpecificServices' method.这不是 EF 定义的服务,因此必须使用“TryAddProviderSpecificServices”方法注册为特定于提供者的服务。

*Edit: I am using Pomelo.EntityFrameworkCore.MySql *编辑:我正在使用 Pomelo.EntityFrameworkCore.MySql

**Edit: I added csproj file code: **编辑:我添加了 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>

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有同样的问题,原来 Pomelo.EntityFrameworkCore.MySql 的当前版本需要 3.1.8 和 5.0.0 之前的 Microsoft.EntityFrameworkCore 版本

Downgrading Microsoft.EntityFrameworkCore to the version before 5.0.0 (3.1.11) solved the problem for me将 Microsoft.EntityFrameworkCore 降级到 5.0.0 (3.1.11) 之前的版本为我解决了问题

I decided to change db to Sql Server.我决定将 db 更改为 Sql Server。 I've removed Pomelo.EntityFrameworkCore.MySql and added Microsoft.EntityFrameworkCore.SqlServer and problem's solved.我已经删除了 Pomelo.EntityFrameworkCore.MySql 并添加了 Microsoft.EntityFrameworkCore.SqlServer 并且问题解决了。

if you want EF Core 5 to work with MySQL you need to install Pomelo.EntityFrameworkCore.MySql 5.0.0-alpha.2如果你想让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" />

or you can downgrade Pomelo.EntityFrameworkCore.MySql to a more stable version which is 3.2.4 along with EF Core 3.1.12或者您可以将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: for more information visit Pomelo.EntityFrameworkCore.MySql compatibility section PS:更多信息请访问Pomelo.EntityFrameworkCore.MySql 兼容性部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 核心 Web API 使用实体框架核心 - ASP.NET Core Web API using Entity Framework Core 在 .net 核心中添加实体时出现问题 - there is a problem adding entity in .net core 通过 .NET Core Web API 在 Entity Framework Core 中 PATCH 实体和相关实体 - PATCH entity and related entities in Entity Framework Core via .NET Core Web API .NET Web API(非核心)-在Entity Framework控制器中创建修补程序操作 - .NET Web API (not Core) - Creating a Patch operation in an Entity Framework controller ASP.NET 核心 Web API - 使用实体框架在 .NET 核心 6 中播种数据 - ASP.NET Core Web API - seeding data in .NET Core 6 using Entity Framework 用谷歌授权 asp.net 核心 web api(实体框架核心) Z7C82E852FB415F250Z92 - authorize asp.net core web api (entity framework core) with google oauth ASP.NET 核心 Web API 与实体框架核心默认脚手架 Z594C103F2C6E04C3DAZ8AB05E 混淆 put? - ASP.NET Core Web API with Entity Framework Core default scaffolded controller confuses put and post? ASP.NET Core Web API &amp; Entity Framework Core; 为什么我收到这个错误? - ASP.NET Core Web API & Entity Framework Core; why am I getting this error? .NET 核心实体框架 - .NET Core Entity Framework .NET实体框架核心 - .NET Entity Framework Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM