error CS1061: 'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer' and no accessible extension method 'UseSqlServer' ...
means the compiler cannot see the SQL Server provider's extension method. Either the
project references only Microsoft.EntityFrameworkCore and not
Microsoft.EntityFrameworkCore.SqlServer, or the file is missing
using Microsoft.EntityFrameworkCore;.
Add the provider package with the same version as your EF Core package. If it is
already in the .csproj, add the using directive at the top of the file
that calls UseSqlServer. Other providers' methods, such as
UseNpgsql, also come from their own packages, so check those first.
The error
Determining projects to restore...
All projects are up-to-date for restore.
...\EfLab\Program.cs(6,13): 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?) [...\EfLab\EfLab.csproj]
Build FAILED.
The project paths are shortened; nothing else in the output was changed.
Why it happens
UseSqlServer is not a method of DbContextOptionsBuilder. It is
an extension method that ships in the provider package, so the core EF Core package
alone has nothing by that name. The compiler's hint covers both possible gaps: "a using
directive or an assembly reference".
The second gap is easy to hit in an ASP.NET Core project. The package's XML
documentation puts AddDbContext in
Microsoft.Extensions.DependencyInjection, which is in the project's
generated GlobalUsings.g.cs, so that call compiles. The SqlServer package
puts UseSqlServer in Microsoft.EntityFrameworkCore, which that
file does not list. When the DbContext class is in its own
file with its own using and Program.cs has none, the package can be
referenced correctly and Program.cs still fails with exactly this CS1061,
at the UseSqlServer line and nowhere else.
The fix
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 10.0.12
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.12" />
When the package is there, the fix is the first line of Program.cs:
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<ClinicDb>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("Clinic")));
Both versions built with 0 errors. Keep the provider and the core package on the same
version; the provider depends on the core package anyway, so referencing only the
provider also works. One thing to know: on this run, adding the SqlServer provider
10.0.12 brought two NuGet audit warnings, NU1902 and NU1903, for a transitive
System.Security.Cryptography.Pkcs 9.0.11 (the build summary counted each
twice, as 4 warnings). They do not
stop the build unless warnings are errors;
the
NU1903 post shows how to lift a transitive package.
How it was reproduced
A dotnet new web project on .NET SDK 10.0.403 (net10.0) with
a ClinicDb context and Microsoft.EntityFrameworkCore 10.0.12
as the only package gave the error above. A second copy with both packages, the
DbContext moved to ClinicDb.cs and no using in
Program.cs gave the same message at Program.cs(4,13). No
database was needed: this is a compile error.
Frequently asked
- Which package contains UseSqlServer in EF Core?
- Microsoft.EntityFrameworkCore.SqlServer. Add it with dotnet add package Microsoft.EntityFrameworkCore.SqlServer using the same version as your Microsoft.EntityFrameworkCore package.
- Why is UseSqlServer not found when the package is installed?
- The file is missing using Microsoft.EntityFrameworkCore. ASP.NET Core implicit usings include the namespace of AddDbContext but not the one that holds the provider extension methods.
- Do I need both Microsoft.EntityFrameworkCore and the SqlServer package?
- No. The SqlServer provider package depends on Microsoft.EntityFrameworkCore, so referencing only Microsoft.EntityFrameworkCore.SqlServer built fine. If you list both, keep them on the same version.
More decoded errors in the Fixes category. If it compiles but fails at run time saying no provider is configured, see No database provider has been configured.