Reputation: 2150
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
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
Reputation: 71
For anyone using VS Code with multiple projects:
Target the project in which you've added Entity Framework.
For Windows:
Upvotes: 0
Reputation: 1590
For .net core 6 it was needing Microsoft.EntityFrameworkCore.Design
Upvotes: 0
Reputation: 500
Same issue after fresh installation EntityFrameworkCore.SQLServer and Design on Visual Studio 2022.
Restarting Visual Studio did the trick!
Upvotes: 0
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
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
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
Reputation: 25
I was facing this issue with Sqlite.
This solved my issue.
Upvotes: 0
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
Reputation: 11
try this
services.AddDbContext(options => options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")))
Upvotes: 0
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
Reputation: 702
Adding using Microsoft.Extensions.DependencyInjection fixed this issue for me.
Upvotes: 3
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
Reputation: 789
Could you please try like this
services.AddDbContext<ClassDbContextName>(options =>
options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
Upvotes: -2