【问题标题】:The type or namespace name 'EntityFrameworkCore' does not exist in the namespace 'Microsoft命名空间“Microsoft”中不存在类型或命名空间名称“EntityFrameworkCore”
【发布时间】:2018-06-14 08:38:59
【问题描述】:

我尝试为 Visual Studio 2017 执行 Razor ContosoUniversity(最初在 ContsoObserve 中重命名)并出现错误 我的 .csproj 是

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Utils" Version="2.0.1" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Update="Microsoft.NETCore.App" Version="2.0.4" />
  </ItemGroup>
  <ItemGroup>
        <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1" />
    </ItemGroup>
</Project>

我在 NuGet 中安装了所有这些包

当我

dotnet aspnet-codegenerator razorpage -m Student -dc SchoolContext -udl -outDir Pages\Students --referenceScriptLibraries

得到

构建失败。 Data\SchoolContext.cs(5,17):错误 CS0234:命名空间“Microsoft”中不存在类型或命名空间名称“EntityFrameworkCore”(您是否缺少程序集引用?) [C:\Users\STR\source\ repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Pages\About.cshtml.cs(5,32):错误 CS0234:命名空间“Microsoft.AspNetCore.Mvc”中不存在类型或命名空间名称“RazorPages”(您是否缺少程序集引用?)[C:\用户\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Pages\Contact.cshtml.cs(5,32):错误 CS0234:命名空间“Microsoft.AspNetCore.Mvc”中不存在类型或命名空间名称“RazorPages”(您是否缺少程序集引用?) [C:\用户\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Pages\Error.cshtml.cs(6,32):错误 CS0234:命名空间“Microsoft.AspNetCore.Mvc”中不存在类型或命名空间名称“RazorPages”(您是否缺少程序集引用?)[C:\用户\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Pages\Index.cshtml.cs(6,32):错误 CS0234:命名空间“Microsoft.AspNetCore.Mvc”中不存在类型或命名空间名称“RazorPages”(您是否缺少程序集引用?) [C:\用户\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Program.cs(7,28):错误 CS0234:命名空间“Microsoft.AspNetCore”中不存在类型或命名空间名称“Hosting”(您是否缺少程序集引用?) [C:\Users\STR\source\ repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Program.cs(8,28):错误 CS0234:命名空间“Microsoft.Extensions”中不存在类型或命名空间名称“Configuration”(您是否缺少程序集引用?) [C:\Users\STR\source\ repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Program.cs(9,28):错误 CS0234:命名空间“Microsoft.Extensions”中不存在类型或命名空间名称“Logging”(您是否缺少程序集引用?) [C:\Users\STR\source\ repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Startup.cs(5,28):错误 CS0234:命名空间“Microsoft.AspNetCore”中不存在类型或命名空间名称“Builder”(您是否缺少程序集引用?) [C:\Users\STR\source\ repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Startup.cs(6,28):错误 CS0234:命名空间“Microsoft.AspNetCore”中不存在类型或命名空间名称“Hosting”(您是否缺少程序集引用?) [C:\Users\STR\source\ repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Startup.cs(7,28):错误 CS0234:名称空间“Microsoft.Extensions”中不存在类型或名称空间名称“Configuration”(您是否缺少程序集引用?) [C:\Users\STR\source\ repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Startup.cs(10,17):错误 CS0234:命名空间“Microsoft”中不存在类型或命名空间名称“EntityFrameworkCore”(您是否缺少程序集引用?) [C:\Users\STR\source\repos\ ContosoObserve\ContosoObserve\ContosoObserve.csproj] Pages\About.cshtml.cs(9,31):错误 CS0246:找不到类型或命名空间名称“PageModel”(您是否缺少 using 指令或程序集引用?) [C:\Users\STR\source \repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Program.cs(39,23):错误 CS0246:找不到类型或命名空间名称“IWebHost”(是否缺少 using 指令或程序集引用?) [C:\Users\STR\source\repos\ContosoObserve \ContosoObserve\ContosoObserve.csproj] Data\SchoolContext.cs(10,34):错误 CS0246:找不到类型或命名空间名称“DbContext”(您是否缺少 using 指令或程序集引用?) [C:\Users\STR\source\repos \ContosoObserve\ContosoObserve\ContosoObserve.csproj] Startup.cs(21,16):错误 CS0246:找不到类型或命名空间名称“IConfiguration”(是否缺少 using 指令或程序集引用?) [C:\Users\STR\source\repos\ContosoObserve \ContosoObserve\ContosoObserve.csproj] Pages\Contact.cshtml.cs(9,33):错误 CS0246:找不到类型或命名空间名称“PageModel”(您是否缺少 using 指令或程序集引用?) [C:\Users\STR\source \repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Startup.cs(33,31):错误 CS0246:找不到类型或命名空间名称“IApplicationBuilder”(是否缺少 using 指令或程序集引用?) [C:\Users\STR\source\repos\ContosoObserve \ContosoObserve\ContosoObserve.csproj] Startup.cs(33,56):错误 CS0246:找不到类型或命名空间名称“IHostingEnvironment”(是否缺少 using 指令或程序集引用?) [C:\Users\STR\source\repos\ContosoObserve \ContosoObserve\ContosoObserve.csproj] Data\SchoolContext.cs(16,16):错误 CS0246:找不到类型或命名空间名称“DbSet”(您是否缺少 using 指令或程序集引用?) [C:\Users\STR\source \repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Startup.cs(16,24):错误 CS0246:找不到类型或命名空间名称“IConfiguration”(是否缺少 using 指令或程序集引用?) [C:\Users\STR\source\repos\ContosoObserve \ContosoObserve\ContosoObserve.csproj] Pages\Error.cshtml.cs(10,31):错误 CS0246:找不到类型或命名空间名称“PageModel”(您是否缺少 using 指令或程序集引用?) [C:\Users\STR\source \repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Data\SchoolContext.cs(17,16):错误 CS0246:找不到类型或命名空间名称“DbSet”(您是否缺少 using 指令或程序集引用?) [C:\Users\STR\source \repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Data\SchoolContext.cs(18,16):错误 CS0246:找不到类型或命名空间名称“DbSet”(您是否缺少 using 指令或程序集引用?) [C:\Users\STR\source \repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Data\SchoolContext.cs(20,49):错误 CS0246:找不到类型或命名空间名称“ModelBuilder”(您是否缺少 using 指令或程序集引用?) [C:\Users\STR\source\repos \ContosoObserve\ContosoObserve\ContosoObserve.csproj] Data\SchoolContext.cs(20,33):错误 CS0115:'SchoolContext.OnModelCreating(ModelBuilder)':找不到合适的方法来覆盖 [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Pages\Index.cshtml.cs(10,31):错误 CS0246:找不到类型或命名空间名称“PageModel”(您是否缺少 using 指令或程序集引用?) [C:\Users\STR\source \repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Data\SchoolContext.cs(12,30):错误 CS0246:找不到类型或命名空间名称“DbContextOptions”(您是否缺少 using 指令或程序集引用?) [C:\Users\STR\source \repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] 0 个警告 28 个错误 我尝试卸载并重新加载项目。结果一样。

我添加 并且错误计数减少

构建失败。

Data\SchoolContext.cs(5,17):错误 CS0234:类型或命名空间名称 命名空间“Microsoft”中不存在“EntityFrameworkCore”(是 你错过了一个装配参考?) [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Pages\About.cshtml.cs(5,32):错误 CS0234:类型或命名空间名称 命名空间中不存在“RazorPages” 'Microsoft.AspNetCore.Mvc'(您是否缺少程序集参考?) [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Pages\Contact.cshtml.cs(5,32):错误 CS0234:类型或命名空间 名称空间中不存在名称“RazorPages” 'Microsoft.AspNetCore.Mvc'(您是否缺少程序集参考?) [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Pages\Error.cshtml.cs(6,32):错误 CS0234:类型或命名空间名称 命名空间中不存在“RazorPages” 'Microsoft.AspNetCore.Mvc'(您是否缺少程序集参考?) [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Pages\Index.cshtml.cs(6,32):错误 CS0234:类型或命名空间名称 命名空间中不存在“RazorPages” 'Microsoft.AspNetCore.Mvc'(您是否缺少程序集参考?) [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Startup.cs(10,17):错误 CS0234:类型或命名空间名称 命名空间“Microsoft”中不存在“EntityFrameworkCore”(是 你错过了一个装配参考?) [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Pages\About.cshtml.cs(9,31):错误 CS0246:类型或命名空间名称 找不到“PageModel”(您是否缺少 using 指令或 装配参考?) [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Data\SchoolContext.cs(10,34):错误 CS0246:类型或命名空间名称 找不到“DbContext”(您是否缺少 using 指令或 装配参考?) [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Pages\Contact.cshtml.cs(9,33):错误 CS0246:类型或命名空间 找不到名称“PageModel”(您是否缺少 using 指令 还是汇编参考?) [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Pages\Error.cshtml.cs(10,31):错误 CS0246:类型或命名空间名称 找不到“PageModel”(您是否缺少 using 指令或 装配参考?) [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Data\SchoolContext.cs(16,16):错误 CS0246:类型或命名空间名称 找不到“DbSet”(您是否缺少 using 指令或 装配参考?) [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Pages\Index.cshtml.cs(10,31):错误 CS0246:类型或命名空间名称 找不到“PageModel”(您是否缺少 using 指令或 装配参考?) [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Data\SchoolContext.cs(17,16):错误 CS0246:类型或命名空间名称 找不到“DbSet”(您是否缺少 using 指令或 装配参考?) [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Data\SchoolContext.cs(18,16):错误 CS0246:类型或命名空间名称 找不到“DbSet”(您是否缺少 using 指令或 装配参考?) [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Data\SchoolContext.cs(20,49):错误 CS0246:类型或命名空间名称 找不到“模型构建器”(您是否缺少 using 指令 还是汇编参考?) [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] 数据\SchoolContext.cs(20,33):错误 CS0115: 'SchoolContext.OnModelCreating(ModelBuilder)':没有合适的方法 发现覆盖 [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] Data\SchoolContext.cs(12,30):错误 CS0246:类型或命名空间名称 找不到“DbContextOptions”(您是否缺少使用 指令还是程序集引用?) [C:\Users\STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj] 0 个警告

【问题讨论】:

  • 您是否完成了dotnet restore 以获取所有 nuget 包?
  • 是的。当然。我添加 并且错误计数从 28 变为 17

标签: c# razor asp.net-core visual-studio-2017


【解决方案1】:

将您的包引用更改为Microsoft.AspNetCore.All(当前版本为 2.0.3)。这将包括实体框架(加上你需要的更多)。

另外,不要在PackageReference 元素上使用Update,使用Include

【讨论】:

  • 找不到指定的框架“Microsoft.NETCore.App”,版本“2.0.4”。 - 检查应用程序依赖项并定位安装在以下位置的框架版本:\ - 或者,安装框架版本“2.0.4”。
  • @ifooi 我不知道你改变了什么?
  • 是的,我添加了 Microsoft.AspNetCore.All 但 Microsoft.NETCore.App" Version="2.0.4 仍然存在。我需要卸载它吗?
  • 只有一个PackageReference
  • 严重代码描述项目文件行抑制状态错误 NU1605 检测到包降级:Microsoft.NETCore.App 从 2.0.3 到 2.0.0。直接从项目中引用包以选择不同的版本。 ContosoObserve -> Microsoft.VisualStudio.Web.CodeGeneration.Tools 2.0.1 -> Microsoft.NETCore.App (>= 2.0.3) ContosoObserve -> Microsoft.NETCore.App (>= 2.0.0) ContosoObserve C:\Users\ STR\source\repos\ContosoObserve\ContosoObserve\ContosoObserve.csproj 1
【解决方案2】:

即使我已经在 Visual Studio 2019 上安装了“Microsoft.EntityFrameworkCore”,我也收到了相同的错误消息。我确实卸载并重新安装,它工作正常。

【讨论】:

    【解决方案3】:

    假设您的 IDE 是 Visual Studio 2017 或 2019,请转到 Nuget 包管理器,搜索并安装以单词 Microsoft.EntityFrameworkCore 开头的包,它们的数量约为 3 个包。错误会消失

    【讨论】:

      猜你喜欢
      • 2018-05-09
      • 2021-07-28
      • 2013-05-04
      • 2015-01-05
      • 1970-01-01
      • 2017-12-29
      • 2014-04-12
      • 1970-01-01
      相关资源
      最近更新 更多