'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.
Solution 1:[1]
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")));
Solution 2:[2]
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;
Solution 3:[3]
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>
Solution 4:[4]
Adding using Microsoft.Extensions.DependencyInjection fixed this issue for me.
Solution 5:[5]
try this
services.AddDbContext(options => options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")))
Solution 6:[6]
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.
Solution 7:[7]
I was facing this issue with Sqlite.
- I installed Microsoft.EntityFrameworkCore.Sqlite from Nuget galary.
- I installed the package under the folder inside of which StartUp.cs file is located.
- Introduced using Microsoft.EntityFrameworkCore inside StartUP.cs
This solved my issue.
Solution 8:[8]
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
Solution 9:[9]
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
Solution 10:[10]
Could you please try like this
services.AddDbContext<ClassDbContextName>(options =>
options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
