Can't reference Entity Framework Core in .NET 5 and Visual Studio Code

Viewed 686

I'm using Visual Studio Code to build a class library with .NET 5 and Entity Framework Core.

However, I can't make reference to Entity Framework Core and I keep getting errors as if it doesn't exist.

This is the project file:

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

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.7" />
  </ItemGroup>

</Project>

All the package reference were included using dotnet add package

These are the attempts I already made:

  • Restart Omni sharp
  • Restore the packages (dotnet package restore)
  • Use a lower version of Entity Framework Core (5.0.0)

The specific problem and error message is here and was here all the time, so no other intelligent editor may close this question again:

The result continues to be the same, I get an error message on the using telling the EntityFrameworkCore namespace doesn't exist in the Microsoft namespace. However, the IntelliSense shows EntityFrameworkCore.

What else could I try?

1 Answers

As explained on the comments of the question, although the error message from build and intellisense points to a specific .cs file, when I use dotnet build on this project no error is displayed.

However, the project is part of a solution where A references B which references C and C is the project with the error.

Building the solution results in errors pointing directly to project C, giving the wrong idea that C is the problem. It's not.

Following the recommendation on the comments and opening the solution in Visual Studio, it was possible to better identify the problem as being on the solution. Once this was done, the solution for the problem was found.

The solution is to add to the main project, A, the references included in project C. The same Entity Framework Core package in project C was included in project A as well and this solved the problem, allowing the solution to compile.

However, this is not a final and correct solution. I received many feedback advising this is not the usual behavior in .NET Core and I know it was not the usual behavior in older .NET versions as well. The mystery is still present: Why am I required to add to project A the same references than project C?

A clue to the mystery may be the fact project A is an Azure Function project. However, I don't know how this affects the entire solution.

Anyway, it's working with the fixed references.

Related