簡體   English   中英

Entity Framework Core 添加到 .Net Core Web Api - IRelationalTypeMappingSource 問題

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

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

有同樣的問題,原來 Pomelo.EntityFrameworkCore.MySql 的當前版本需要 3.1.8 和 5.0.0 之前的 Microsoft.EntityFrameworkCore 版本

將 Microsoft.EntityFrameworkCore 降級到 5.0.0 (3.1.11) 之前的版本為我解決了問題

我決定將 db 更改為 Sql Server。 我已經刪除了 Pomelo.EntityFrameworkCore.MySql 並添加了 Microsoft.EntityFrameworkCore.SqlServer 並且問題解決了。

如果你想讓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 兼容性部分

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM