我在 Visual Studio 2017 中有 .NET Core 2 项目。我正在尝试添加 (Postgresql) 数据库连接。这是一个代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext(options =>
options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
// Add framework services.
services.AddMvc();
}
但是编译器提示这个消息:
IServiceCollection does not contain a definition for 'AddDbContext' and no extension method 'AddDbContext' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)
我安装了 NuGet 包 Npgsql。我还尝试安装 NuGet 包 EntityFramework,但我收到错误消息:
包恢复失败。回滚“MyProject”的包更改。
这是我问题的根源吗?我应该安装其他库吗?
在此question使用了过程 AddEntityFramework() 和 AddEntityFrameworkNpgsql(),但这两个在我的项目中也不被编译器识别。
最佳答案
例如确保你安装了正确版本的相关 NuGet 包
Microsoft.EntityFrameworkCore v3
并且正在使用正确的命名空间,例如
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
关于c# - AddDbContext 在 .NET Core 的 IServiceCollection 中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45032132/