[英]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.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.MySql
兼容性部分
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.