简体   繁体   English

EntityFrameworkCore 在命名空间 Microsoft 中不存在

[英]EntityFrameworkCore does not exist in the namespace Microsoft

I am trying to walk through this tutorial here.我正在尝试在这里浏览本教程。

https://learn.microsoft.com/en-us/as.net/core/data/ef-mvc/intro https://learn.microsoft.com/en-us/as.net/core/data/ef-mvc/intro

However after successfully installing EntityFrameworkCore in the package manager console using the command:然而,在 package 管理器控制台成功安装 EntityFrameworkCore 后,使用以下命令:

Install-Package Microsoft.EntityFrameworkCore.SqlServer安装包 Microsoft.EntityFrameworkCore.SqlServer

then running a do.net restore successfully in the cmd, the project does not register EntityFrameworkCore.然后在cmd运行do.net restore成功,项目没有注册EntityFrameworkCore。 In the.csproj file you can see the line在 .csproj 文件中,您可以看到该行

<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.不幸的是,每当我在文件中添加带有 Microsoft.EntityFrameworkCore 的 using 语句时,我都会收到标题中提到的错误。 Any idea why this might be happening?知道为什么会这样吗?

I solved this problem by:我通过以下方式解决了这个问题:

(1) Right clicking the project in the Solution Explorer (1) 在解决方案资源管理器中右键单击项目

(2) Clicking unload project (2) 点击卸载项目

(3) Click edit the .csproj and check if there is a Package Reference to EF (3) 点击edit the .csproj并检查是否有对EF的Package Reference

(4) Right clicking the project again in the Solution Explorer (4) 在解决方案资源管理器中再次右键单击项目

(5) Then clicked reload project (5) 然后点击reload project

Now it recognizes EntityFrameworkCore and there are no more build errors现在它可以识别 EntityFrameworkCore 并且不再有构建错误

In Visual Code:在视觉代码中:

Ctrl + Shift + P => Restart OmniSharp Ctrl + Shift + P =>重新启动 OmniSharp

转到 NuGet 浏览器并安装 Microsoft.EntityFrameworkCore!

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:你错过了一个必需的 Nuget 包,根据microsoft ,你总共需要安装 3 个包,但在我的情况下,它只需要 2 个包,如下所示:

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

Hope this can solve your problem.希望这可以解决您的问题。

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.将以下代码从https://github.com/aspnet/Docs/tree/master/aspnetcore/tutorials/first-web-api/sample/TodoApi复制到 TodoApi.csproj 对我有用。

<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 Microsoft.AspNetCore.All 可能过多,但它包括 EntityFrameworkCore 等

If using VS Code, the solution that worked for me:如果使用 VS Code,对我有用的解决方案:

In the terminal type:在终端类型中:

dotnet add package Microsoft.EntityFrameworkCore

After adding package, Ctrl + Shift + P -> Restart OmniSharp添加包后, Ctrl + Shift + P ->重启OmniSharp

  1. Go to Tools in your visual studio » NuGet Package Manager » Package Manager Console转到 Visual Studio 中的工具 » NuGet 包管理器 » 包管理器控制台
  2. type->类型->

    install-package microsoft.entityframeworkcore.sqlserver安装包 microsoft.entityframeworkcore.sqlserver

In visual Studio, go to -> Project -> Manage NuGet Packages.. ->在 Visual Studio 中,转到 ->项目->管理 NuGet 包.. -> 在视觉

Select Microsoft Entity framework Core .选择Microsoft 实体框架 Core The click on Add package on the bottom right corner button.单击右下角按钮上的添加包

在此处输入图片说明

Clean up the .csproj file so if you see something like...清理 .csproj 文件,如果您看到类似...

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

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.从关于 package.config 文件的其他评论之一,仔细检查......似乎,我的问题似乎是 package.config 文件中的 targetFramework="net472" 与项目的 .Net 版本不匹配。 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 :)该项目使用的是 4.7.2,但包文件是“471”,将 package.config 中的 targetFramework 更新为“472”,问题就消失了:)

Seem in the past never had many issues with mixing 4.xy, as long as 4.x were the same no troubles, as of late, sure seems .Net want 4.xy to all match...似乎过去在混合 4.xy 时从来没有遇到过很多问题,只要 4.x 是相同的就没有问题,最近看来 .Net 确实希望 4.xy 全部匹配......

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

我不得不将实体框架的版本从 6.2 降低到 6.0

I am assuming this is the same issue I encountered with: https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-6.0我假设这与我遇到的问题相同: https : //docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-6.0

The getting started directions include a section that lead you to believe you need to add the following -入门指南包括一个部分,让您相信您需要添加以下内容 -

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using RazorPagesMovie.Data;
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

builder.Services.AddDbContext<RazorPagesMovieContext>(options => 
       options.UseSqlServer(builder.Configuration.GetConnectionString("RazorPagesMovieContext")));

var app = builder.Build();

However I determined that if you skip this and continue the tutorial, it is handled later on...但是我决定如果你跳过这个并继续本教程,它会在稍后处理......

Use the nugget packege manager on VS code and add Microsoft.EntityFrameworkCore that way and the issue will be solved it will also place it into the.csproj在 VS 代码上使用 nugget packege 管理器并以这种方式添加 Microsoft.EntityFrameworkCore 问题将得到解决,它还将把它放入.csproj

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.16"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 命名空间“Microsoft”中不存在“EntityFrameworkCore” - 'EntityFrameworkCore' does not exist in the namespace 'Microsoft' 命名空间“Microsoft.EntityFrameworkCore”中不存在“迁移” - 'Migrations' does not exist in the namespace 'Microsoft.EntityFrameworkCore' 命名空间“Microsoft”中不存在类型或命名空间名称“EntityFrameworkCore” - The type or namespace name 'EntityFrameworkCore' does not exist in the namespace 'Microsoft Microsoft名称空间中不存在ApplicationInsight - ApplicationInsight does not exist in the namespace Microsoft 命名空间“SharePoint”在命名空间“Microsoft”中不存在 - Namespace 'SharePoint' does not exist in the namespace 'Microsoft' Microsoft.Reporting 在命名空间中不存在 - Microsoft.Reporting does not exist in namespace 命名空间“Microsoft.VisualBasic”中不存在类型或命名空间名称“FileIO” - The type or namespace name 'FileIO' does not exist in the namespace 'Microsoft.VisualBasic' 类型或名称空间名称“ DirectX”在名称空间“ Microsoft” Windows 8.1中不存在 - The type or namespace name 'DirectX' does not exist in the namespace 'Microsoft' Windows 8.1 命名空间“Microsoft”中不存在类型或命名空间名称“设备” - The type or namespace name 'Devices' does not exist in the namespace 'Microsoft' 类型或名称空间CodeAnalysis在名称空间&#39;Microsoft.SqlServer.Dac中不存在 - The type or namespace CodeAnalysis does not exist in the namespace 'Microsoft.SqlServer.Dac
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM