[英]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.cs
的ConfigureServices
方法中...
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: 您有两种解决方法:
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.