An accurate depiction of a NuGet package factory

How to solve ‘DbContextOptionsBuilder’ does not contain a definition for ‘UseSqlServer’ error in .NET Core?

This post was most recently updated on July 14th, 2022.

2 min read.

This is another, kind of a classic and simple solution to a fairly simple problem: How to fix it, when your DbContextOptionsBuilder fails to take in your connection string and complains about UseSqlServer missing or whatever?

Okay – let’s take a step back. How did we end up here?

So, let’s set the scene. You’re building your DbContext (called ApplicationDbContext in the example below) in a .NET Core application of some sorts. You’ll do this like shown below:

using Microsoft.EntityFrameworkCore;
using System;
 
namespace YourNamespace
{
    public class YourClass
    {
        private string _cs = "Server=(localdb)\\mssqllocaldb;Database=aspnet-mydb;Trusted_Connection=True;MultipleActiveResultSets=True";

        public YourClass()
        {
           var options = new DbContextOptionsBuilder<ApplicationDbContext>()
                   .UseSqlServer(_cs)
                   .Options;

            using (var ctx = new ApplicationDbContext(options))
            {
               // Your code here
            }
         }
    }
}

If you want to learn more about how to build and configure a DbContext, see this article:

How to instantiate your DbContext outside your Data project?

However, in this particular case, you might run into an issue where your code fails to build with an error like below:

Error CS1061
'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer' and no accessible extension method 'UseSqlServer' accepting a first argument of type 'DbContextOptionsBuilder' could be found (are you missing a using directive or an assembly reference?)

So, essentially, during the build UseSqlServer() is not found. And you could get the same error when instantiating a DbContextOptions object and calling its UseSqlServer property.

What do?

Solution – find the NuGet package that provides “UseSqlServer()” 😏

The solution is usually fairly simple. Chances are you’re just missing a nuget package: Microsoft.EntityFrameworkCore.SqlServer

Time needed: 10 minutes

How to resolve error “‘DbContextOptionsBuilder’ does not contain a definition for ‘UseSqlServer'”?

  1. Add the package “Microsoft.EntityFrameworkCore.SqlServer” to your project

    That’s typically a really, really simple fix!

    Either use NuGet Package Manager to find a package called “Microsoft.EntityFrameworkCore.SqlServer“, or run this in Package Manager Console:

    Install-Package Microsoft.EntityFrameworkCore.SqlServer

    However, for whatever reason, that doesn’t always work. I’ve had the package manager fail silently and the Package Manager console command above never finish.

  2. If the above didn’t work, modify your NuGet.config / .csproj file directly

    In case the above step didn’t work, you can always just edit your nuget configuration file. Or if you’re on .NET Core, by editing the project file like shown below:

    "Edit Project File" in Visual Studio Solution Explorer

  3. Add the package to the file directly

    And then, add the line for the package like shown below (your required version might not be 3.0.0, though – compare to packages starting with something like Microsoft.EntityFrameworkCore to figure out the desired version)!

    For example, on .NET Core, modifying the .csproj file looks something like this:

    <Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <IsPackable>false</IsPackable>
    </PropertyGroup>

    <ItemGroup>
    <!-- You'll probably have quite a few other nuget packages right around here: -->
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0">
    <!-- You need to add the line below: -->
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />
    </ItemGroup>

    </Project>

  4. Save & Restore

    Next: save, and then restore the packages – shown in the screenshot below:

    "Restore NuGet Packages" in the context menu in Visual Studio's solution explorer


After a while (after it’s finished restoring), you should be able to build/package your solution again. And you should be golden! ☺

mm
4.7 23 votes
Article Rating
Subscribe
Notify of
guest

22 Comments
most voted
newest oldest
Inline Feedbacks
View all comments