Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'DbContextOptionsBuilder' does not contain a definition for UseNpgsql()

I am facing an issue while giving the connectionstring to get it connect to PostgreSQL through the AddDbContext() method in ConfigureServices method of Startup.cs

I'm getting this error while calling the function options.UseNpgsql() (see the image below).

I have seen other questions regarding this but those are for usesqlserver() and also they did not solve my problem.

enter image description here

like image 211
Karthik Saxena Avatar asked Mar 05 '18 18:03

Karthik Saxena


People also ask

Does not contain a definition of UseSqlServer?

To solve this error "'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer'", you should add the Sqlite package. Open your CMD > Go to the path of the project folder, then run this command : dotnet add package Microsoft.EntityFrameworkCore.Sqlite.

What is dbContextOptions?

The dbContextOptions carries the configuration information needed to configure the DbContext. The dbContextOptions can also be configured using the OnConfiguring method. This method gets the DbContextOptionsBuilder as its argument. It is then used to create the dbContextOptions.


1 Answers

Two things to check:

  1. Are you missing a reference to the Npgsql.EntityFrameworkCore.PostgreSQL Nuget package?
  2. Are you missing the correct import. The UseNpgsql() extension method is in the Microsoft.EntityFrameworkCore namespace which means you should have this line:

    using Microsoft.EntityFrameworkCore;
    
like image 172
DavidG Avatar answered Sep 20 '22 09:09

DavidG