DBContextBuilder doesnot contain a definition for UseSqlServer in .NET 6

Viewed 61

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

2 Answers

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;


Related