c# - AddDbContext 在 .NET Core 的 IServiceCollection 中不可用

标签 c# .net postgresql .net-core visual-studio-2017

我在 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/

相关文章:

c# - 优雅地关闭系统托盘应用程序

c# - 一次阻塞收集过程 n 个项目 - 一旦完成 1 个就继续

java - 如何在 jdbc postgresql 准备语句中多次使用相同的值

c# - 在数据库中更新时未保存嵌套的拥有类型

C# 通过强制转换运算符进行动态转换

C#:带有以 Child 类型作为参数的方法的继承类:调用了错误的方法

c# - 使用 DocumentPaginator 打印时如何打印预览?

c# - 如何找到需要引用哪个程序集才能使用某个命名空间?

postgresql - 从 jsonb 字段构建 jsonb 数组

ruby-on-rails - Postgres DB 上的整数超出范围