An accurate depiction of a NuGet package factory

Solving: ‘DbContextOptionsBuilder’ does not contain a definition for ‘UseSqlServer’

This post was most recently updated on June 1st, 2020.

Reading Time: 2 minutes.

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 _connectionString = "Server=(localdb)\\mssqllocaldb;Database=aspnet-mydb;Trusted_Connection=True;MultipleActiveResultSets=True";
 
        public YourClass()
        {
           var options = new DbContextOptionsBuilder<ApplicationDbContext>()
                   .UseSqlServer(_connectionString)
                   .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:

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?)

What do?

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

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

That’s typically a really, really simple fix. Either use NuGet Package Manager (not abbreviated “NPM”, mind you) to find a package called “Microsoft.EntityFrameworkCore.SqlServer”, or run this in the 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.

In that case, 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
“Edit Project File” in Visual Studio Solution Explorer

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)!

<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  ... :D Something like below: -->
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0">
    <!-- Add the line below: -->
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />
  </ItemGroup>
 
</Project>

Next: save, and then restore the packages:

"Restore NuGet Packages" in the context menu in Visual Studio's solution explorer
“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
0 0 vote
Article Rating
Subscribe
Notify of
guest
14 Comments
Inline Feedbacks
View all comments
Sweety

Thank you

eddy

Thank you