无法在 .NET 5 和 Visual Studio Code 中引用 Entity Framework Core [英] Can't reference Entity Framework Core in .NET 5 and Visual Studio Code

查看:59
本文介绍了无法在 .NET 5 和 Visual Studio Code 中引用 Entity Framework Core的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Visual Studio Code 构建一个包含 .NET 5 和 Entity Framework Core 的类库.

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

但是,我无法引用 Entity Framework Core,并且我不断收到错误,就好像它不存在一样.

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

这是项目文件:

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

使用 dotnet add package

这些是我已经做过的尝试:

These are the attempts I already made:

  • 重启OmniSharp
  • 恢复包 (dotnet package restore)
  • 使用较低版本的 Entity Framework Core (5.0.0)

具体问题和错误信息都在这里,一直都在这里,所以没有其他智能编辑器可能会再次关闭这个问题:

结果仍然相同,我在 using 上收到一条错误消息,告诉 EntityFrameworkCore 命名空间在 Microsoft 命名空间中不存在.但是,IntelliSense 显示EntityFrameworkCore.

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.

我还能尝试什么?

推荐答案

正如在问题的评论中所解释的,虽然来自 build 和 intellisense 的错误消息指向特定的 .cs 文件,但当我使用 dotnet build 在这个项目上没有显示错误.

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.

然而,该项目是一个解决方案的一部分,其中 A 引用 B 引用 C 和 C 是有错误的项目.

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

构建解决方案导致错误直接指向项目 C,从而错误地认为 C 是问题所在.不是.

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

按照评论中的建议并在 Visual Studio 中打开解决方案,可以更好地将问题识别为解决方案中的问题.一旦完成,就找到了问题的解决方案.

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.

解决方案是将项目C中包含的引用添加到主项目A中.项目C中的相同Entity Framework Core包也包含在项目A中,这解决了问题,使解决方案能够编译.

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.

然而,这不是最终正确的解决方案.我收到了很多反馈,认为这不是 .NET Core 中的常见行为,我知道这也不是旧 .NET 版本中的常见行为.谜团仍然存在:为什么我需要向项目 A 添加与项目 C 相同的引用?

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 是一个 Azure Function 项目.但是,我不知道这对整个解决方案有何影响.

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.

这篇关于无法在 .NET 5 和 Visual Studio Code 中引用 Entity Framework Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆