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!

mm

3
Leave a Reply

avatar
5000
2 Comment threads
1 Thread replies
2 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
mmSylvain GantoisSuchi Siwach Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Suchi Siwach
Guest
Suchi Siwach

Thank you for sharing this article.

Sylvain Gantois
Guest
Sylvain Gantois

Hi, thanks, this put me on the right track. Since it is changing all the time, there is now a
Microsoft.EntityFrameworkCore.SqlServer nuget package. Cheers.