How to fix 'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer'
By Tan Lee Published on Jun 11, 2024 1.76K
The error message "'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer' and no accessible extension method 'UseSqlServer'" typically occurs when the necessary NuGet package for Entity Framework Core SQL Server is not installed or not properly referenced in your project.
Error
Severity Code Description Project File Line Suppression State 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?)
The error occurs because the UseSqlServer
method is not recognized by the DbContextOptionsBuilder
. This typically means the required Microsoft.EntityFrameworkCore.SqlServer
package is missing from the project.
To resolve this issue, you need to ensure that you have the necessary packages installed and properly configured in your project.
Make sure you have installed the package for SQL Server provider.
You can do that by right-clicking on your project, then select Manage Nuget Packages => Search for 'Microsoft.EntityFrameworkCore.SqlServer' and install it to your ASP.Net Core project.
PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer
Ensure that you have the correct using
directive at the top of your C# file where you are configuring the DbContextOptionsBuilder
.
using Microsoft.EntityFrameworkCore;
Manually delete the Bin, Obj, package, and .vs directories in your project to resolve potential build or dependency issues.
Then, right-click on the project to clean and rebuild it.
- How to Initialize TagHelpers in ASP.NET Core with Shared Data
- Boost Your ASP.NET Core Website Performance with .NET Profiler
- The name 'Session' does not exist in the current context
- Implementing Two-Factor Authentication with Google Authenticator in ASP.NET Core
- How to securely reverse-proxy ASP.NET Core
- How to Retrieve Client IP in ASP.NET Core Behind a Reverse Proxy
- Only one parameter per action may be bound from body in ASP.NET Core
- The request matched multiple endpoints in ASP.NET Core