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

I am new to EF core and I am trying to get it to work with my main asp.net project.

I get the above error in my startup.cs when trying to confiugure dbcontext to use the connection string from config. I follow this guide: https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro

Problem code 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 usesqlserver recognized if I put it directly in 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 online search queries point to missing links, but I can’t find which one is missing (see image). links

Any help would be greatly appreciated.

thanks

+61
asp.net-core entity-framework-core
source share
17 answers

This is a known issue in the project system. See dotnet / project-system # 1741

+10
source share

We install the NuGet package for Microsoft.EntityFrameworkCore.SqlServer.

Microsoft.EntityFrameworkCore.SqlServer

 PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer 

Then

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

Update do not forget to add a namespace using Microsoft.EntityFrameworkCore;

+152
source share

add using Microsoft.EntityFrameworkCore;

manually solved the problem for me

Found that here

+60
source share

Install below NuGet package will solve your problem

Microsoft.EntityFrameworkCore.SqlServer

Microsoft.EntityFrameworkCore.SqlServer installation package

+20
source share

I used Visual Studio Code.

1) Try installing the package "Microsoft.EntityFrameworkCore.SqlServer", indicating 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'

The package link Microsoft.EntityFrameworkCore.SqlServer "is incompatible with" all "frameworks in the project " for this.

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

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

3) If you find any dependency problem for "Microsoft.EntityFrameworkCore.SqlServer.Design", for example, the package " Microsoft.EntityFrameworkCore.Design" is incompatible with "all" the frameworks in the project "", we need to run the following command:

VS Code: -

dotnet add package Microsoft.EntityFrameworkCore.Design -v 1.1

Visual studio

Install-package Microsoft.EntityFrameworkCore.Design -v 1.1

+7
source share

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

+6
source share

Follow the instructions 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) ); 
+5
source share

I believe that this can be solved by adding a link to the project in Microsoft.EntityFrameworkCore.SqlServer.Design

 Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design 

Microsoft.EntityFrameworkCore.SqlServer was not directly installed in my project, but the .Design package will still install it as a necessary condition.

+2
source share

add Add Install-Package Microsoft.EntityFrameworkCore.SqlServer

then add .cs to your file using Microsoft.EntityFrameworkCore;

finally add this to your main Startup.cs file

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

I went around this simply:

Add SqlServerDbContextOptionsExtensions to the class in question Allow SqlServerDbContextOptionsExtensions

This fixes the problem, by default there should be no link.

+1
source share

For those who still have this problem: use NuGet to install: Microsoft.EntityFrameworkCore.Proxies

This issue is related to using Castle Proxy with EFCore.

+1
source share

If you encounter this problem in the case of Sqlite, then

I think this is a problem with the Sqlite version, I had the same problem when I used these SqLite versions

Version 2.2.4 :

enter image description here

After checking the version here enter image description here I changed the version, after which it earned .

enter image description here

No errors after using this

Version 2.1.2 :

enter image description here

+1
source share

As noted in Win's answer with the most points, you may need to install the Microsoft.EntityFrameworkCore.SqlServer NuGet package, but note that this question uses the asp.net mvc kernel. In the latest version of ASP.NET Core 2.1, MS included a so-called meta package called Microsoft.AspNetCore.App

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

You can see the link to it if you right-click the ASP.NET Core MVC project in Solution Explorer and select Edit Project File

You should see this metapackage if the main ASP.NET web application uses 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;

+1
source share

Copying the following code into TodoApi.csproj from https://github.com/aspnet/Docs/tree/master/aspnetcore/tutorials/first-web-api/sample/TodoApi resolved a 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 includes EntityFrameworkCore

0
source share

For asp.net core version 2.1, be sure to add the following package to solve the problem. (At least this will solve the problem using SQLite)

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

Here is a link to documentation using SQLite with an entity platform engine. https://docs.microsoft.com/en-us/ef/core/get-started/netcore/new-db-sqlite

0
source share

I had to use the line

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

in the ConfigureServices method in Startup.cs

0
source share

Wow so many answers, but no one has mentioned this package Microsoft.EntityFrameworkCore.InMemory !

Add a link to this package: <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.2.2"/> and everything will be fine.

0
source share

All Articles