【发布时间】:2019-04-17 16:11:41
【问题描述】:
我有一个带有控制器的 .Net Core WebApi 项目,该控制器调用服务、类库以获取数据。该服务使用实体框架,而不是 EFCore。错误失败,这里:
public IEnumerable<TEntity> GetAll()
{
return _dbContext.Set<TEntity>().AsNoTracking().ToList();
}
这是错误:
System.NotSupportedException: '无法确定'System.Data.SqlClient.SqlClientFactory' 类型的提供程序工厂的提供程序名称。确保在应用程序配置中安装或注册了 ADO.NET 提供程序。'
我知道这是可以做到的,这是根据本文将 .NetCore 与 EF 结合使用的首选方式:https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6?view=aspnetcore-2.2
“在 ASP.NET Core 应用程序中使用 Entity Framework 6 的推荐方法是将 EF6 上下文和模型类放在以完整框架为目标的类库项目中。从 ASP 添加对类库的引用。 NET Core 项目。请参阅带有 EF6 和 ASP.NET Core 项目的示例 Visual Studio 解决方案。"
这是微软提供的示例:https://github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/data/entity-framework-6/sample
这是我的 appsettings.json 文件:
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=Savi- Development;Trusted_Connection=True;"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
这是我的 startup.cs:
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddScoped<ApplicationDbContext>(_ => new ApplicationDbContext(Configuration.GetConnectionString("DefaultConnection")));
services.AddScoped<IRegionService, RegionService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
}
这是我认为正确的 project.cs 文件。它引用了完整的实体框架。
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.2.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Savi.Services\Savi.Services.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="EntityFramework.SqlServer">
<HintPath>..\..\Savi.Services\bin\Debug\EntityFramework.SqlServer.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
我没有 project.fragment.json 文件。
【问题讨论】:
-
你能分享你的 appsettings.json 和 startup.cs 吗?
-
.net core 还不支持实体框架
-
这个解决方案有两个项目,一个api core另一个net framework类库。
标签: c# entity-framework .net-core