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

Multi tool use
Multi tool use


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



I'm 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 confiugure the dbcontext to use a connection string from config. I'm following this tutorial : https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro


startup.cs



The problematic 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 is recognized if I put it directly into the context :


usesqlserver


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 reasearch online points to missing references, but I can't seem to find out which one i'm missing(see image).references



Any help would be greatly appreciated,



Thanks





Same thing, intellissense does not find the method either.
– Maxime Laflamme
Mar 30 '17 at 16:36




8 Answers
8



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





Thanks, got around the problem by going back to core targeting .net framework.
– Maxime Laflamme
Mar 31 '17 at 18:58



We install Microsoft.EntityFrameworkCore.SqlServer NuGet Package.



Microsoft.EntityFrameworkCore.SqlServer


PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer



Then,


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





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 ...
– Maxime Laflamme
Mar 29 '17 at 16:37






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.
– H35am
Dec 14 '17 at 13:40





H35am is correct, the real answer is this one.
– Breadtruck
Mar 19 at 3:48



adding
using Microsoft.EntityFrameworkCore;


using Microsoft.EntityFrameworkCore;



manually solved the problem for me



Found that here





This solved it for me, thanks.
– SsjCosty
Apr 9 at 13:52





This is the answer.
– smulholland2
Apr 16 at 20:41



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



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.





Installing unnecessary package which brings in required project as transitive dependency is not a solution to the issue.
– Smit
Mar 30 '17 at 1:08





You assume the package is unnecessary, yet it fixed my project when I had the exact same issue.
– Andrew S
Mar 31 '17 at 21:53





That package is unnecessary for the current question. It may be required for different issue but not this one.
– Smit
Mar 31 '17 at 21:57





Adding this reference solved my problem as well
– csharp_beginner
Jul 21 '17 at 17:18





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.
– Rus
Feb 5 at 14:44



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



I got around this by simply:



Add SqlServerDbContextOptionsExtensions to the class in question
Resolve SqlServerDbContextOptionsExtensions


SqlServerDbContextOptionsExtensions


SqlServerDbContextOptionsExtensions



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



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





Thanks paper111, I'll update a response with the code for a solution
– Bruno Pereira
Jul 1 at 2:31







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

wGQbU30NqvuV,Zg,YLXK9ibvGKv4b2JF8cjr3P06y Ri1ahK,AkYUn6rzYQWS,cb2h dU VATjtks8 6OYd pEz,1,jH YuRPUdM
E,exvnokUtwvV,HxSc0x4pTAgpM2jaj0I4HOtq1j Wde W S eeM WT4ixPoXuyOiaOT3aobdD4wIaRdGI QhQrxwHJw 4UJ,wrGO2,k r

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters