397

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).

2
  • Same thing, intellissense does not find the method either. Commented Mar 30, 2017 at 16:36
  • This is a known issue in the project system. See dotnet/project-system#1741 Commented Mar 31, 2017 at 16:47

36 Answers 36

845

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"))); 
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the answer but I already did that. I'm starting to suspect something is corrupted in my original project. I will try to see if I can reproduce from a new project ...
This answer should be marked as correct answer not the other ones, reason is that the method UseSqlServer is from this package and not the other packages.
This isn't working for me. I have nuget packages: Microsoft.EntityFrameworkCore, Microsoft.EntityFrameworkCore.Design, Microsoft.EntityFrameworkCore.SqlServer, Microsoft.EntityFrameworkCore.Tools and "using Microsoft.EntityFrameworkCore;" at the top but still get the error
You also need this constructor public AspDbContext(DbContextOptions<AspDbContext> options) : base(options) { }
For future users who face this issue, the version of the nuget package matters. It wasn't able to resolve for Microsoft.EntityFrameworkCore.SqlServer 6.0.1 for me when .NET core version was 5.0.0. I had to install a lower version of the nuget package for it to resolve the red line error.
|
143

adding using Microsoft.EntityFrameworkCore;

manually solved the problem for me

Found that here

Edit...

for dotnet core 3.1 add

Microsoft.EntityFrameworkCore.SqlServer

4 Comments

This is the answer.
Fixed it for me! Somehow it wasn't in the suggestions list ctrl+.
Fixed it for me in 2019 with .Net Core 2.2
Yes, but I found that the Microsoft.EntityFrameworkCore.SqlServer package was needed for dotnetcore 3.1. Prior to that, it appeared that Microsoft.EntityFrameworkCore alone was enough.
48

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) ); 

2 Comments

This part of the solution where you are manually adding the using statement is what worked for me - seems my editor didnt detect the package in the assembly
Worked for me and you do need to manually add the using statement as the IDE doesn't provide a hint.
42

Install below NuGet Package will solve your issue

Microsoft.EntityFrameworkCore.SqlServer

Install-Package Microsoft.EntityFrameworkCore.SqlServer

3 Comments

Add a little bit more explanation about how this one line command will solve the op's problem. Specifically, here you are addressing a package so you need to explain how that package works.
"DbContextOptionsBuilder.UseSqlServer" is available in Microsoft.EntityFrameworkCore.SqlServer package, So need to add the reference of it to solve the build issue.
Yes I also encoutered the same problem, after I have installed this package from NuGet Microsoft.EntityFrameworkCore.SqlServer, it resolved the issue. I do not know the reason though.
25

EntityFramework UseSqlServer Solved

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

2 Comments

installing Microsoft.EntityFrameworkCore.SqlServer solved it for me
package Microsoft.EntityFrameworkCore.SqlServer and Microsoft.SqlServer.Server solved my problem.
11

I believe this can be solved by adding a project reference to Microsoft.EntityFrameworkCore.SqlServer.Design

Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design 

Microsoft.EntityFrameworkCore.SqlServer wasn't directly installed in my project, but the .Design package will install it anyway as a prerequisite.

5 Comments

Installing unnecessary package which brings in required project as transitive dependency is not a solution to the issue.
You assume the package is unnecessary, yet it fixed my project when I had the exact same issue.
That package is unnecessary for the current question. It may be required for different issue but not this one.
As of today, July 27th, you can install just Microsoft.EntityFrameworkCore.SqlServer package and it will solve the issue
As of today, 5/Feb/2018, you still need to add the Microsoft.EntityFrameworkCore.SqlServer.Design package. This was observed using an ASP.NET Core 2.0.1 web project.
10

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

Install-Package Microsoft.EntityFrameworkCore.SqlServer 

Comments

10

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

Comments

8

Install the following packages from Nuget :-

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

Comments

7

I was using Visual Studio Code.

1) Try to install the package 'Microsoft.EntityFrameworkCore.SqlServer' by specifying the version number.

VS Code:

'dotnet add package Microsoft.EntityFrameworkCore.SqlServer -v 1.1.1'

Visual Studio:-

'Install-Package Microsoft.EntityFrameworkCore.SqlServer -v 1.1.1'

Refer the link 'Package 'Microsoft.EntityFrameworkCore.SqlServer' is incompatible with 'all' frameworks in the project' for doing it.

2) Then add the namespace 'using Microsoft.EntityFrameworkCore;' manually in the Startup.cs file.

Refer the below link https://github.com/aspnet/EntityFramework/issues/7891.

3) If you get any dependency issue for 'Microsoft.EntityFrameworkCore.SqlServer.Design', like "Package 'Microsoft.EntityFrameworkCore.Design' is incompatible with 'all' frameworks in project" ,we need to run the below command,

VS Code:-

dotnet add package Microsoft.EntityFrameworkCore.Design -v 1.1

Visual Studio

Install-Package Microsoft.EntityFrameworkCore.Design -v 1.1

Comments

7

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

Comments

5

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

Microsoft.EntityFrameworkCore.SqlServer 

Comments

5

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

Install-Package Microsoft.EntityFrameworkCore.SqlServer -v 1.1.1

Comments

5

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"))); 

Comments

4

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;

Comments

4

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.

Comments

4

Install package, EntityFrameworkCore.SqlServer:

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

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

1 Comment

4

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

Comments

3

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.

Comments

3

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

Comments

3

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"))); } 

Comments

2

For anyone still having this problem: Use NuGet to install: Microsoft.EntityFrameworkCore.Proxies

This problem is related to the use of Castle Proxy with EFCore.

1 Comment

This fixed the problem for me. It is also mentioned at learn.microsoft.com "The simplest way to use lazy-loading is by installing the Microsoft.EntityFrameworkCore.Proxies package and enabling it with a call to UseLazyLoadingProxies." https://learn.microsoft.com/en-us/ef/core/querying/related-data/lazy
2

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.

Comments

2

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

Comments

2

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.

Comments

1

I got around this by simply:

Add SqlServerDbContextOptionsExtensions to the class in question Resolve SqlServerDbContextOptionsExtensions

This fixes the issue, must be missing some reference by default.

Comments

1

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.

Comments

1

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

Comments

0

For asp.net core version 2.1 make sure to add the following package to fix the problem. (At least this fix the issue using SQLite)

dotnet add package Microsoft.EntityFrameworkCore.Sqlite dotnet add package Microsoft.EntityFrameworkCore.Design 

Here is the reference of the documentation using SQLite with entity framework core. https://learn.microsoft.com/en-us/ef/core/get-started/netcore/new-db-sqlite

Comments

0

I had to use the line

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

in the ConfigureServices method in the Startup.cs

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.