You are currently viewing [Solved] DbContextOptionsBuilder does not contain a definition for ‘usesqlserver’
ASP.NET Core

[Solved] DbContextOptionsBuilder does not contain a definition for ‘usesqlserver’

Error: DbContextOptionsBuilder does not contain a definition for ‘usesqlserver’ and no extension method ‘usesqlserver’

If you are new with Entity Framework Core and create a new project with blank template then you may face this issue while creating DbContext.

Before start with this solution make sure you have constructor with DbContextOptions as parameter. Similar to below sample code:

 public class EmployeeDbContext: DbContext
    {
        public EmployeeDbContext(DbContextOptions<EmployeeDbContext> options)
        : base(options)
        {
        }
        public DbSet<Employee> Employees { get; set; }
    }

Solution:

First we need to install the Microsoft.EntityFrameworkCore.SqlServer NuGet Package:

Here is the steps to install Nuget package using Package Manager Console

  1. Select the Tools > NuGet Package Manager > Package Manager Console menu command.
  2. Once the console opens, check that the Default project drop-down list shows the project into which you want to install the package. If you have a single project in the solution, it is already selected.
  3. Enter the command: The console window shows the output for the command. Errors typically indicate that the package isn’t compatible with the project’s target framework.
Install-Package Microsoft.EntityFrameworkCore.SqlServer

Once you install the Nuget package you need to import the namespace:

using Microsoft.EntityFrameworkCore;

Enjoy now you can use database context similar to below sample code:

 services.AddDbContext<EmployeeDbContext>(option => option.UseSqlServer(Configuration["database:connection"]));