Uros
Uros

Reputation: 2150

AddDbContext not available in IServiceCollection in .NET Core

I have .NET Core 2 project in Visual Studio 2017. I am trying to add (Postgresql) database connection. Here is a code:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext(options =>
        options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));

    // Add framework services.
    services.AddMvc();
}

But compiler complains with this message:

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?)

I installed NuGet package Npgsql. I also tried to install NuGet package EntityFramework, but I'm receiving error:

Package restore failed. Rolling back package changes for 'MyProject'.

Is this the root of my problem? Should I install some other library?

On this question procedures AddEntityFramework() and AddEntityFrameworkNpgsql() are used, but those two are also not recognized by compiler in my project.

Upvotes: 26

Views: 42581

Answers (14)

avr-girl
avr-girl

Reputation: 557

Here are the packages to install via Nuget, as per Microsoft documentation

To sum it up:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;

var builder = WebApplication.CreateBuilder(args);

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));

var app = builder.Build();

Upvotes: 1

vali
vali

Reputation: 71

For anyone using VS Code with multiple projects:

Target the project in which you've added Entity Framework.

For Windows:

  1. Ctrl+Shift+P
  2. OmniSharp: Select Project (select the project in which you added Entity Framework)

Upvotes: 0

Иво Недев
Иво Недев

Reputation: 1590

For .net core 6 it was needing Microsoft.EntityFrameworkCore.Design

Upvotes: 0

Micha&#235;l Randria
Micha&#235;l Randria

Reputation: 500

Same issue after fresh installation EntityFrameworkCore.SQLServer and Design on Visual Studio 2022.

Restarting Visual Studio did the trick!

Upvotes: 0

ScareCrow
ScareCrow

Reputation: 525

Use the following command to add the specific version of PostgreSQL package to your project

dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL --version 6.0.0

The following command will add the latest package.

dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL

Upvotes: 0

Moorthi Daniel
Moorthi Daniel

Reputation: 157

for ASP.Net core, from command prompt.

dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL

for installing specific version, dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL --version 6.0.0

Upvotes: 0

Dindar
Dindar

Reputation: 3245

In case your persistence layer is in another project (Class Library) and you have installed an appropriate NuGet library like Npgsql.EntityFrameworkCore.PostgreSQL on the persistence layer, Then a build is necessary, especially if you are using VS Code. otherwise, it may not recognize services.AddDbContext<T>

Upvotes: 2

Anupam datta
Anupam datta

Reputation: 25

I was facing this issue with Sqlite.

  1. I installed Microsoft.EntityFrameworkCore.Sqlite from Nuget galary.
  2. I installed the package under the folder inside of which StartUp.cs file is located.
  3. Introduced using Microsoft.EntityFrameworkCore inside StartUP.cs

This solved my issue.

Upvotes: 0

Waleed003
Waleed003

Reputation: 1

Solution is very simple you just have to install microsoft.entityframeworkcore nuget package in the right file. then After the installation the intellisence help the option will be visible to you.

Upvotes: 0

user11255153
user11255153

Reputation: 11

try this

services.AddDbContext(options => options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")))

Upvotes: 0

Bashir Momen
Bashir Momen

Reputation: 1681

Make sure you installed related NuGet packages with the right version for example

Microsoft.EntityFrameworkCore v3

and are using the right namespace such as

using Microsoft.EntityFrameworkCore;

using Microsoft.Extensions.DependencyInjection;

Upvotes: 36

pwned555
pwned555

Reputation: 702

Adding using Microsoft.Extensions.DependencyInjection fixed this issue for me.

Upvotes: 3

Uros
Uros

Reputation: 2150

I installed Npgsql.EntityFrameworkCore.PostgreSQL and that resolved my issue. I also used Danijel's suggestion:

services.AddDbContext<ClassDbContextName>(options =>
            options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));

Upvotes: 3

Danijel Boksan
Danijel Boksan

Reputation: 789

Could you please try like this

services.AddDbContext<ClassDbContextName>(options =>
        options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));

Upvotes: -2

Related Questions