An accurate depiction of a NuGet package factory

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

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

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 = "This comes from your configuration file";
 
        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 building a DbContext, see this article:

However, 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

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

That’s typically a simple fix. Either use NuGet Package Manager, 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:

And then, add the line for the package like shown below (your required version might not be 3.0.0, though – compare to packages 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 -->
 
    <!-- Add the line below: -->
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />
  </ItemGroup>
 
</Project>

Next: save, and then restore the packages:

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

Antti K. Koskela

Antti Koskela is a proud digital native nomadic millennial full stack developer (is that enough funny buzzwords? That's definitely enough funny buzzwords!), who works as a Solutions Architect for Valo Intranet, the product that will make you fall in love with your intranet. Working with the global partner network, he's responsible for the success of Valo deployments happening all around the world.

He's been a developer from 2004 (starting with PHP and Java), and he's been bending and twisting SharePoint into different shapes since MOSS. Nowadays he's not only working on SharePoint, but also on .NET projects, Azure, Office 365 and a lot of other stuff.

This is his personal professional (e.g. professional, but definitely personal) blog.
mm

Leave a Reply

avatar
5000
  Subscribe  
Notify of