[英]Can't reference Entity Framework Core in .NET 5 and Visual Studio Code
I'm using Visual Studio Code to build a class library with .NET 5 and Entity Framework Core.我正在使用 Visual Studio Code 构建一个带有 .NET 5 和 Entity Framework Core 的类库。
However, I can't make reference to Entity Framework Core and I keep getting errors as if it doesn't exist.但是,我无法引用 Entity Framework Core,并且我不断收到错误,就好像它不存在一样。
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
使用
dotnet add package
包含了所有包参考
These are the attempts I already made:这些是我已经做过的尝试:
dotnet package restore
)dotnet package restore
) 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.结果仍然相同,我收到一条关于using的错误消息,告诉 Microsoft 命名空间中不存在EntityFrameworkCore命名空间。 However, the IntelliSense shows EntityFrameworkCore .
但是,智能感知显示EntityFrameworkCore 。
What else could I try?我还能尝试什么?
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.正如问题的评论所解释的那样,虽然来自 build 和 intellisense 的错误消息指向特定的 .cs 文件,但当我在这个项目上使用dotnet build 时,没有显示错误。
However, the project is part of a solution where A references B which references C and C is the project with the error.但是,该项目是解决方案的一部分,其中 A 引用 B 引用 C 和 C 是有错误的项目。
Building the solution results in errors pointing directly to project C, giving the wrong idea that C is the problem.构建解决方案会导致错误直接指向项目 C,从而错误地认为 C 是问题所在。 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.按照对评论的建议并在 Visual Studio 中打开解决方案,可以更好地将问题识别为解决方案中的问题。 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.解决方案是将包含在项目 C 中的引用添加到主项目 A 中。项目 C 中的相同 Entity Framework Core 包也包含在项目 A 中,这解决了问题,允许该解决方案编译。
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.
我收到了很多反馈,建议这不是 .NET Core 中的常见行为,我知道这也不是旧 .NET 版本中的常见行为。 The mystery is still present: Why am I required to add to project A the same references than project C?
谜团仍然存在:为什么我需要向项目 A 添加与项目 C 相同的引用?
A clue to the mystery may be the fact project A is an Azure Function project.谜底的线索可能是项目 A 是一个 Azure Function 项目。 However, I don't know how this affects the entire solution.
但是,我不知道这如何影响整个解决方案。
Anyway, it's working with the fixed references.无论如何,它正在使用固定引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.