Maxime Laflamme
Maxime Laflamme

Reputation: 4086

Entity Framework Core: DbContextOptionsBuilder does not contain a definition for 'usesqlserver' and no extension method 'usesqlserver'

I am new to EF core and I'm trying to get it to work with my ASP.NET Core project.

I get the above error in my startup.cs when trying configure the DbContext to use a connection string from config. I am following this tutorial.

The problematic code is in startup.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using tracV2.models;
using tracV2.data;

namespace tracV2
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc();

            services.AddSingleton<IConfiguration>(Configuration);

            string conn = Configuration.GetConnectionString("optimumDB");

            services.AddDbContext<tracContext>(options => options.usesqlserver(conn));
        }

The UseSqlServer method is recognized if I put it directly into the context:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace tracV2.data
{
    public class tracContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("myrealconnectionstring");
        }

All my research online points to missing references, but I can't seem to find out which one I am missing (see image).

Upvotes: 393

Views: 423384

Answers (30)

user8604852
user8604852

Reputation: 1

I had this issue, it seems that I hadn't added the required NuGet packages, although I thought I had done so, make sure to check them, one by one.

Upvotes: 0

mostafa omer
mostafa omer

Reputation: 51

In my Case in .Net6, After I installed (Microsoft.EntityFrameworkCore.SqlServer, Microsoft.EntityFrameworkCore, Microsoft.EntityFrameworkCore.Tools) I had to Install "Microsoft.EntityFrameworkCore.Design" To Get the definition Of UseSqlServer() function.

Upvotes: 0

NH Dalim
NH Dalim

Reputation: 352

Install-Package:

Microsoft.EntityFrameworkCore.SqlServer

then add the top of your class:

using Microsoft.EntityFrameworkCore;

Then add Datacontext

builder.Services.AddDbContext<LibraryDBContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

Upvotes: 4

Rhushikesh Baradkar
Rhushikesh Baradkar

Reputation: 271

EntityFramework UseSqlServer Solved

Install-Package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore Install-Package Microsoft.EntityFrameworkCore.SqlServer

Upvotes: 25

jeremyjone
jeremyjone

Reputation: 313

I add SqlServer and Sqlite to my project, the same problem arises.

For me, I installed Microsoft.EntityFrameworkCore earlier, it's version is 5.0.6. Now Microsoft.EntityFrameworkCore.SqlServer version is 5.0.7. There is no UserSqlServer method after installation.

1. Try to upgrade the tool version to be consistent

Try to modify the version in csproj:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.7">

Or open NuGet to update the package.

2. Restart your Visual Studio

If it won't help you, the most important thing is to restart VS

And then, everything is OK.

reference https://github.com/dotnet/efcore/issues/7891

Upvotes: 4

Avinash
Avinash

Reputation: 326

In my case :- I have hit the below command and it is resolved. Command

Install-Package Microsoft.EntityFrameworkCore.SqlServer -v 1.1.1

Upvotes: 5

MalachiteBR
MalachiteBR

Reputation: 571

I read and did everything instructed in every answer and I just found out my problem was a missing constructor on my DbContext

public Context(DbContextOptions<Context> options) : base(options) { }

I hope this helps anyone facing the same problem I was.

Upvotes: 2

Nithya
Nithya

Reputation: 17

I installed these three of them in the package manager console and it worked.

Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design -Version 3.1.4 Install-Package Microsoft.EntityFrameworkCore.Tools -Version 3.1.8 Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.8

Upvotes: -1

Saukhya Pol
Saukhya Pol

Reputation: 81

Install the following packages from Nuget :-

  1. Microsoft.EntityFrameworkCore
  2. Microsoft.EntityFrameworkCore.Sqlite.Core

Upvotes: 8

Jay Bharat
Jay Bharat

Reputation: 889

your solution works great.

When I saw this video till 17 Minute: https://www.youtube.com/watch?v=fom80TujpYQ I was facing a problem here:

 services.AddDbContext<PaymentDetailContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("DevConnection")));

UseSqlServer not recognizes so I did this Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.5

& using Microsoft.EntityFrameworkCore;

Then my problem is solved. About me: basically I am a purely PHP programmer since beginning and today only I started .net coding, thanks for good community in .net

Upvotes: 10

Topal Penguen
Topal Penguen

Reputation: 21

Project is works in DotNET Core 3.1+ or higher(future)

Add this package:

  1. NuGet: Microsoft.EntityFrameworkCore.Tools
  2. Project Config (.csproj): <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.8">

Upvotes: 0

Chamath Viduranga
Chamath Viduranga

Reputation: 145

I also had the same problem. I added the following. It works for me

Microsoft.EntityFrameworkCore.SqlServer

Upvotes: 5

surendrapanday
surendrapanday

Reputation: 548

I had same issue but problem went off after going back and fixing DbContext incorrect syntax issue such as it should have been ExampleDbContextClass: DbContext whereas I had missed DbContext part in Context Class where you define your DbSet. Also, I verified following dependencies are needed in order to achieve connection to SqlServer. <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.6" /> Also verify that Version you install is less than your project's current version to be on safe side. Such as if your project is using 3.1 do not try to use newer one which would be for example 3.1.9. I had issue with that too.

Upvotes: 0

Bruno Pereira
Bruno Pereira

Reputation: 721

Follow the steps below.

Install Entity Framework Core Design and SQL Server database provider for Entity Framework Core:

dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer

Import Entity Framework Core:

using Microsoft.EntityFrameworkCore;

And configure your DbContext:

var connectionString = Configuration.GetConnectionString("myDb");
services.AddDbContext<MyDbContext>(options => 
    options.UseSqlServer(connectionString)
);

Upvotes: 46

kiwi7
kiwi7

Reputation: 51

Copying the following code into the TodoApi.csproj from https://github.com/aspnet/Docs/tree/master/aspnetcore/tutorials/first-web-api/sample/TodoApi solved similar issue for me.

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
  </ItemGroup>

</Project>

Microsoft.AspNetCore.All may be excessive but it includes EntityFrameworkCore

Upvotes: 0

Blain Ellis
Blain Ellis

Reputation: 61

I had to use the line

 services.AddEntityFrameworkSqlite().AddDbContext<MovieContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MovieContext")));

in the ConfigureServices method in the Startup.cs

Upvotes: 0

Win
Win

Reputation: 62260

First we install the Microsoft.EntityFrameworkCore.SqlServer NuGet Package:

PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer

Then, after importing the namespace with

using Microsoft.EntityFrameworkCore;

we add the database context:

services.AddDbContext<AspDbContext>(options =>
    options.UseSqlServer(config.GetConnectionString("optimumDB")));

Upvotes: 835

Jaydeep Shil
Jaydeep Shil

Reputation: 1959

first add Install-Package Microsoft.EntityFrameworkCore.SqlServer

next add in your .cs file using Microsoft.EntityFrameworkCore;

finally add this in your core Startup.cs

  public void ConfigureServices(IServiceCollection services)
        {
            services.AddEntityFrameworkSqlServer().AddDbContext<ApplicationContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MovieContext")));
        }

Upvotes: 3

Willie Cheng
Willie Cheng

Reputation: 8253

Easy way to fix this issue

Error message:
enter image description here

Solution:
to install "microsoft.entityframeworkcore.sqlserver" with NuGet
enter image description here

Fixed :
enter image description here

PS: make sure you have using EF on the content "using Microsoft.EntityFrameworkCore;" enter image description here

Upvotes: 3

Install package, EntityFrameworkCore.SqlServer:

PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.3

Nuget: https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer/

Upvotes: 4

Eric Conklin
Eric Conklin

Reputation: 747

Currently working with Entity Framework Core 3.1.3. None of the above solutions fixed my issue.

However, installing the package Microsoft.EntityFrameworkCore.Proxies on my project fixed the issue. Now I can access the UseLazyLoadingProxies() method call when setting my DBContext options.

Hope this helps someone. See the following article:

Lazy Loading in EF Core

Upvotes: 1

Wen Qin Yap
Wen Qin Yap

Reputation: 59

In Visual Studio, check the NuGet Package Manager => Manage Packages for Solution, check all this packages, whether got installed in your solution or not, as below:

  1. EntityFrameworkCore
  2. Microsoft.EntityFrameworkCore
  3. Microsoft.EntityFrameworkCore.InMemory
  4. Microsoft.EntityFrameworkCore.Relational
  5. Microsoft.EntityFrameworkCore.Sqlite.Core
  6. Microsoft.EntityFrameworkCore.SqlServer
  7. Microsoft.EntityFrameworkCore.Tools

I solved the same issues after check all the above packages have been installed.

Upvotes: 4

Victor.Uduak
Victor.Uduak

Reputation: 2824

adding using Microsoft.EntityFrameworkCore;

manually solved the problem for me

Found that here

Edit...

for dotnet core 3.1 add

Microsoft.EntityFrameworkCore.SqlServer

Upvotes: 143

Daniel Melo
Daniel Melo

Reputation: 558

The Package is missing. Open Package Manager Console and execute the code below:

Install-Package Microsoft.EntityFrameworkCore.SqlServer 

Upvotes: 10

Askar
Askar

Reputation: 5854

For me this issue happened with Visual Studio Code and I was able to fix with 2 steps:

  1. Manually adding using Microsoft.EntityFrameworkCore;
  2. Running dotnet build in terminal.

Upvotes: 3

Ian Gibblet
Ian Gibblet

Reputation: 582

I had this trouble when I moved to Microsoft.EntityFrameworkCore.SqlServer v3.0.0 and Microsoft.EntityFrameworkCore.Tools v3.0.0

When I changed back to v2.2.6 on both libraries, the error went away. This is more of a workaround than a solution but it'll get you up and running till the issue is fixed.

Upvotes: 1

Hevski
Hevski

Reputation: 1871

As mentioned by top scoring answer by Win you may need to install Microsoft.EntityFrameworkCore.SqlServer NuGet Package, but please note that this question is using asp.net core mvc. In the latest ASP.NET Core 2.1, MS have included what is called a metapackage called Microsoft.AspNetCore.App

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/metapackage-app?view=aspnetcore-2.2

You can see the reference to it if you right-click the ASP.NET Core MVC project in the solution explorer and select Edit Project File

You should see this metapackage if ASP.NET core webapps the using statement

<PackageReference Include="Microsoft.AspNetCore.App" />

Microsoft.EntityFrameworkCore.SqlServer is included in this metapackage. So in your Startup.cs you may only need to add:

using Microsoft.EntityFrameworkCore;

Upvotes: 4

TAHA SULTAN TEMURI
TAHA SULTAN TEMURI

Reputation: 5151

If you are facing this issue in case of Sqlite then

. I think this is the problem with version of Sqlite,I had the same problem when I was using this versions of SqLite

Version 2.2.4:

enter image description here

After checking version here enter image description here I changed the version then it worked.

enter image description here

No error after using this

Version 2.1.2:

enter image description here

Upvotes: 2

pso
pso

Reputation: 889

Wow so many answers yet none mentioned this Microsoft.EntityFrameworkCore.InMemory package!

Add the reference to this package: <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.2.2" /> and you should be good to go.

Upvotes: 2

Bugay Sarikaya
Bugay Sarikaya

Reputation: 166

Project -> ManageNugetPackages -> Browse -> Search "Microsoft.EntityFrameworkCore.SqlServer" and install or update.

Upvotes: 7

Related Questions