gpt4 book ai didi

c# - Entity Framework 核心添加到.Net核心Web Api-IRelationalTypeMappingSource问题

转载 作者:行者123 更新时间:2023-12-03 16:12:51 26 4
gpt4 key购买 nike

我有.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)运行时,它在此部分给出了异常(exception):( :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)之前的版本可以解决我的问题

关于c# - Entity Framework 核心添加到.Net核心Web Api-IRelationalTypeMappingSource问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63577896/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com