简体   繁体   English

在.NET Core 2.2中找不到Microsoft.Extensions.EntityFrameworkCore命名空间

[英]Microsoft.Extensions.EntityFrameworkCore namespace not found in .NET Core 2.2

I'm reading the book "Pro ASP.NET Core MVC 2" and following his samples, and have reached the point where he introduces EF Core. 我正在阅读《 Pro ASP.NET Core MVC 2》一书,并按照他的示例进行学习,并且已经达到了介绍EF Core的地步。

He says to add the following to the .csproj file... 他说要将以下内容添加到.csproj文件中...

<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" 
                        Version="2.0.0" />

...and then add the following to the ConfigureServices method in Startup.cs ... ...然后将以下内容添加到Startup.csConfigureServices方法中...

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(
        Configuration["Data:SportStoreProducts:ConnectionString"]));

Problem is that when I add the line to my .csproj file, I get a message in the output window saying... 问题是,当我将行添加到.csproj文件时,在输出窗口中显示一条消息,提示...

C:\\Program Files\\dotnet\\sdk\\2.2.104\\Sdks\\Microsoft.NET.Sdk\\targets\\Microsoft.NET.ObsoleteReferences.targets(33,5): Warning NETSDK1059: The tool 'Microsoft.EntityFrameworkCore.Tools.DotNet' is now included in the .NET Core SDK. C:\\ Program Files \\ dotnet \\ sdk \\ 2.2.104 \\ Sdks \\ Microsoft.NET.Sdk \\ targets \\ Microsoft.NET.ObsoleteReferences.targets(33,5):警告NETSDK1059:工具'Microsoft.EntityFrameworkCore.Tools.DotNet现在已包含在.NET Core SDK中。 Information on resolving this warning is available at ( https://aka.ms/dotnetclitools-in-box ). 有关解决此警告的信息,请访问( https://aka.ms/dotnetclitools-in-box )。

OK, so I removed the line from the .csproj file again, and got a compiler error in Startup.cs file: OK,所以我再次从.csproj文件中删除了该行,并在Startup.cs文件中出现了编译器错误:

Type ApplicationDbContext could not be found. 找不到类型ApplicationDbContext

I added a using for the namespace Microsoft.Extensions.EntityFrameworkCore , but that caused a compiler error saying it could not be found. 我为命名空间Microsoft.Extensions.EntityFrameworkCore添加了using,但是这导致了编译器错误,表明找不到该错误。 I took the offer (from R# I think) to search Nuget for it, but it didn't come up with anything 我接受了报价(我认为来自R#)来搜索Nuget,但没有提出任何建议

Anyone any ideas? 有任何想法吗? Other than downgrading to 2.0, I don't know how to proceed. 除了降级到2.0,我不知道如何进行。

UPDATE OK, so I'm daft. 更新好,所以我很傻。 I read the book in bed, then tried to do the sample code the following day, and thinking that I knew the content, skim-read it whilst building the project. 我躺在床上看书,然后第二天尝试编写示例代码,并以为我知道内容,在构建项目时略读了内容。 I must have missed the two code snippets where he showed the changes. 我一定错过了他展示更改的两个代码段。 All my fault, not the book's. 我所有的错,不是这本书的错。

The bad thing about ASP.NET Core books, so far that is, is that they become obsolete quite quickly. 到目前为止,关于ASP.NET Core书籍的坏事是它们很快就过时了。

This page explains that the <DotNetCliToolReference> references for dotnet ef commands are now part of the SDK, so those aren't needed anymore. 此页面说明, dotnet ef命令的<DotNetCliToolReference>引用现在已成为SDK的一部分,因此不再需要它们。

As for not being able to find the class ApplicationDbContext , that's because the project template used did not use ASP.NET Core Identity for Individual User Accounts and either the book did not explain that the class needs to be manually created or you didn't follow it thoroughly. 至于找不到类ApplicationDbContext ,这是因为所使用的项目模板未对单个用户帐户使用ASP.NET Core标识,并且该书没有说明该类需要手动创建,或者您未遵循它彻底。 You have two options for fixing this: 您有两种解决方法:

  • Recreate the project using ASP.NET Core Identity. 使用ASP.NET Core Identity重新创建项目。 Steps here , or 步骤在这里 ,或
  • Create that class which is just this: 创建该类就是这样:

     public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { } 

    Note, however, that if the book does not show this, you will have to install a series of ASP.NET Core Identity packages, Entity Framework Core packages, configure them all in the Startup class and create the ApplicationUser class, if I'm not missing anything. 但是请注意,如果书中没有显示此内容,则必须安装一系列ASP.NET Core标识程序包,Entity Framework Core程序包,并在Startup类中对其进行配置,并创建ApplicationUser类。不丢失任何东西。 Quite a big amount of tedious work. 大量繁琐的工作。

暂无
暂无

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

相关问题 EntityFrameworkCore 在命名空间 Microsoft 中不存在 - EntityFrameworkCore does not exist in the namespace Microsoft 命名空间“Microsoft”中不存在“EntityFrameworkCore” - 'EntityFrameworkCore' does not exist in the namespace 'Microsoft' .Net core 3:Microsoft.Extensions.Logging.Internal 中不存在命名空间 internal - .Net core 3: The namespace internal does not exist in Microsoft.Extensions.Logging.Internal 将 .Net Core 项目从 3.1 降级到 2.2 - 找不到类型或命名空间名称“IWebHostEnvironment” - Downgrade .Net Core project from 3.1 to 2.2 - The type or namespace name 'IWebHostEnvironment' could not be found 在.NET Standard项目中找不到Microsoft.EntityFrameworkCore类型 - Type not found Microsoft.EntityFrameworkCore in .NET Standard project Asp.net 核心 Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: - Asp.net Core Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: 无法构建.Net core 2.2项目? 找不到类型或名称空间名称“系统” - Cannot build .Net core 2.2 project? The type or namespace name 'System' could not be found .Net核心和EntityFrameworkCore - .Net Core And EntityFrameworkCore 命名空间“Microsoft.EntityFrameworkCore”中不存在“迁移” - 'Migrations' does not exist in the namespace 'Microsoft.EntityFrameworkCore' 在 asp.net core 2.2 中找不到类型或命名空间名称“Worker”(您是否缺少 using 指令或程序集引用?)? - The type or namespace name 'Worker' could not be found (are you missing a using directive or an assembly reference?) in asp.net core 2.2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM