#SharePointProblems | Koskila.net

Solutions are worthless unless shared! Antti K. Koskela's Personal Professional Blog

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

koskila
Reading Time 4 min
Word Count 567 words
Comments 22 comments
Rating 4.7 (23 votes)
View

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?

AI-powered summary: Efficiently Solve ‘DbContextOptionsBuilder’ Error in .NET Core

The ‘DbContextOptionsBuilder’ error in .NET Core arises when setting up DbContext. The ‘UseSqlServer’ method is not found during the build. The solution is to add the ‘Microsoft.EntityFrameworkCore.SqlServer’ NuGet package to your project. This package provides the ‘UseSqlServer’ method, resolving the error and allowing the DbContext to accept the connection string.

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

Comments

Interactive comments not implemented yet. Showing legacy comments migrated from WordPress.
Suchi Siwach
2020-03-20 21:44:24)
Thank you for sharing this article.
2020-03-24 00:17:25
Thanks for your comment, Suchi! Happy to hear it was useful ☺️
Sylvain Gantois
2020-03-21 06:18:55)
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.
2020-04-18 22:04:59
Thanks for the comment! Yeah, it keeps changing... Trying to keep the article up-to-date, though! :)
Zaulo
2020-04-03 05:13:12)
Graicas Muchas gracias
Pedro Felipe
2020-04-08 00:26:14)
Do u know the rason why the EntityFrameworkCore.SqlServer don't install together with EntityFrameworkCore when we install it?
2020-04-08 11:29:52
Hi Pedro, Thanks for your question - it's a very valid one. Now, I don't have any secret information about this, so this is just an educated guess... But with .NET & EF Core, Microsoft has definitely adopted a far, far more granular approach to their dependencies than before. Since at the time of installation, Microsoft doesn't know what kind of a Database will you be connecting to - SQL, CosmosDb, MySQL or something else - they don't want to reference unnecessary providers. Now it's up to the developer to know what they need, which in an optimal scenario should drive the final size of the packaged application down. To be fair, I suppose this optimization could be resolved during build as well, but getting rid of unnecessary dependencies is a good aim. That's what I figured anyway :)
Nader Saoudy
2020-04-14 03:19:39)
Thanks
2020-04-18 22:03:57
My pleasure!
Sweety
2020-04-16 13:52:48)
Thank you
2020-04-17 00:28:01
Happy to be of help! ☺️
eddy
2020-06-04 10:35:49)

Thank you

Selva
2021-04-05 13:55:44)
it's working fine
NETB
2021-04-07 18:14:19)
Thanks very much!!!!!
Furat
2021-09-27 13:37:55)
thank you very much you are helped me to solve my problem
Antti K. Koskela
2021-10-03 22:57:15
Thanks for your comment, happy to hear it was helpful! :)
James
2022-08-18 14:11:59)
You are life saver! Thanx!!!!!
2022-08-18 18:21:25
Happy to be of help, James!
Peter
2022-10-20 17:53:58)
Perhaps you may have an answer to this question. My EntityFramework connects to the database, but I get only empty sets (no data). Thanks. I am puzzled.  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)        => optionsBuilder.UseSqlServer("myString") I used scaffold builder to create this.
Antti K. Koskela
2023-03-12 20:12:02
Hi Peter, In EF Core, I'd start by actually checking your connection string, and after that reviewing the SQL your query actually generates (by calling .ToQueryString() on your non-evaluated query, or using ctx.Database.Log like this). If both of those seem ok, make sure your code isn't somehow parsing and discarding your results - can't recall the specifics, but been there and done that at least once. Let me know how you actually ended up solving it! 😅
Whitewater Magpie Ltd.
© 2025
Static Site Generation timestamp: 2025-08-26T05:15:52Z