numb
numb

Reputation: 63

DBContextBuilder doesnot contain a definition for UseSqlServer in .NET 6

I'm trying to implement microservices in .NET core framework (version 6.0) and facing this particular issue while adding the services of DBContext in the Program.CS file.

statement I'm using :

builder.Services.AddDbContext<ProductContext>(options=>options.UseSqlServer(builder.Configuration.GetConnectionString("ProductDB")));

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?) Micro D:\NET_Micro\Micro\Micro\Program.cs 7 Active

Upvotes: 2

Views: 3950

Answers (3)

Sachil Krishna
Sachil Krishna

Reputation: 1

Seems there was a bug in VisualStudio 2022, had to restart and reinstall below packages to get it working:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;

Upvotes: 0

void
void

Reputation: 106

These Errors occur usually when you are not including certain packages in your code. Try downloading the following NuGet packages by running below mentioned code in your visual studio's package manager console :

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

dotnet add package Microsoft.EntityFrameworkCore.Design

and include them in your startup.cs/program.cs :

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;
using Microsoft.EntityFrameworkCore.Design;


Upvotes: 2

Qing Guo
Qing Guo

Reputation: 9152

Try to install the Microsoft.EntityFrameworkCore.SqlServer NuGet Package.

Then, import the namespace with

using Microsoft.EntityFrameworkCore;

Upvotes: 2

Related Questions