AddDbContext is not available in IServiceCollection in .NET Core

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

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

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

      

But the compiler is complaining about this message:

IServiceCollection does not contain a definition for 'AddDbContext' and no extension method 'AddDbContext' that takes a first argument of type "IServiceCollection" can be found (are you missing a directive or assembly reference?)

I have installed the NgGet NuGet package. I also tried to install the NuGet package EntityFramework, but I get the error:

Failed to restore package. Rollback package changes for "MyProject".

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

This question uses AddEntityFramework () and AddEntityFrameworkNpgsql (), but those two are also not recognized by the compiler in my project.

+3


source to share


2 answers


I installed Npgsql.EntityFrameworkCore.PostgreSQL and solved this problem. I also used Daniel's suggestion:



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

      

+1


source


Could you try like this



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

      

-2


source







All Articles