30
votes

I am trying to walk through this tutorial here.

https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro

However after successfully installing EntityFrameworkCore in the package manager console using the command:

Install-Package Microsoft.EntityFrameworkCore.SqlServer

then running a dotnet restore successfully in the cmd, the project does not register EntityFrameworkCore. In the .csproj file you can see the line

<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />

Unfortunately whenever I add a using statement with Microsoft.EntityFrameworkCore in a file I get the error mentioned in the title. Any idea why this might be happening?

11
Just to say, it's now July 2017, and this code is still a bit flakey. If you get errors like this, do try restarting VS2017. I know, I know... it's a bit ridiculous, but that did fix a few ASP.Net Core issues for me today.. and all I was doing was following Microsoft's own tutorial. docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/…Mike Gledhill

11 Answers

55
votes

I solved this problem by:

(1) Right clicking the project in the Solution Explorer

(2) Clicking unload project

(3) Click edit the .csproj and check if there is a Package Reference to EF

(4) Right clicking the project again in the Solution Explorer

(5) Then clicked reload project

Now it recognizes EntityFrameworkCore and there are no more build errors

14
votes

In Visual Code:

Ctrl + Shift + P => Restart OmniSharp

7
votes

Go to NuGet browser and install Microsoft.EntityFrameworkCore!

5
votes

You missed a required Nuget Package, according to microsoft, you will need to install 3 packages in total, but in my case, it is only required 2 packages as the following:

  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools

Hope this can solve your problem.

3
votes

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

3
votes

If using VS Code, the solution that worked for me:

In the terminal type:

dotnet add package Microsoft.EntityFrameworkCore

After adding package, Ctrl + Shift + P -> Restart OmniSharp

1
votes
  1. Go to Tools in your visual studio » NuGet Package Manager » Package Manager Console
  2. type->

    install-package microsoft.entityframeworkcore.sqlserver

1
votes

In visual Studio, go to -> Project -> Manage NuGet Packages.. -> In Visual

Select Microsoft Entity framework Core. The click on Add package on the bottom right corner button.

enter image description here

0
votes

Clean up the .csproj file so if you see something like...

<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.4">
   <PrivateAssets>all</PrivateAssets>
   <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>

Change it to :

<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.4" />
0
votes

From one of the other comments about the package.config file, double checking... Seems, my issue appeared that the targetFramework="net472" in the package.config file didn't match the project's .Net version. The project is using 4.7.2 but the package file was "471", updating the targetFramework in the package.config to "472" the problems went away :)

Seem in the past never had many issues with mixing 4.x.y, as long as 4.x were the same no troubles, as of late, sure seems .Net want 4.x.y to all match...

`<packages>
   <package id="EntityFramework" version="6.2.0" targetFramework="net472" />
</packages>`
0
votes

I had to lower the version of Entity Framework from 6.2 to 6.0