Thêm Entity Framework Core vào .Net Core Web Api - Sự cố IRelationalTypeMappingSource

Aug 25 2020

Tôi có .Net Core web API hoạt động tốt. Khi tôi cố gắng thêm Entity Framework Core, đầu tiên là lỗi biên dịch. Nó nói rằng, tôi phải thêm Microsoft.Bcl.AsyncInterfaces mặc dù tôi đã sử dụng .Net Core 3.1. Khi tôi thêm điều này, được biên dịch tốt nhưng khi api chạy, nó đưa ra ngoại lệ này. Tôi không thể tìm thấy bất kỳ giải pháp nào cho ngoại lệ này trên internet:

Khi chạy api web lõi .net (gỡ lỗi):

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

Mã khởi động hoạt động tốt. Khi lớp dbContext (PKDbContext) của tôi chạy, nó đưa ra ngoại lệ ở phần này: ( : base (options) )

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

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

Đã ném ngoại lệ: 'System.InvalidOperationException' trong Microsoft.EntityFrameworkCore.dll Một ngoại lệ của loại 'System.InvalidOperationException' đã xảy ra trong Microsoft.EntityFrameworkCore.dll nhưng không được xử lý trong mã người dùng Nhà cung cấp cơ sở dữ liệu đã cố gắng đăng ký triển khai 'IRelationalTypeMappingSource' dịch vụ. Đây không phải là một dịch vụ được xác định bởi EF và như vậy phải được đăng ký như một dịch vụ dành riêng cho nhà cung cấp bằng phương pháp 'TryAddProviderSpecificServices'.

* Chỉnh sửa: Tôi đang sử dụng Pomelo.EntityFrameworkCore.MySql

** Chỉnh sửa: Tôi đã thêm mã tệp 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>

Trả lời

2 compazizo Sep 09 2020 at 18:09

Tôi quyết định thay đổi db thành Sql Server. Tôi đã xóa Pomelo.EntityFrameworkCore.MySql và thêm Microsoft.EntityFrameworkCore.SqlServer và sự cố đã được giải quyết.