Microsoft.Extensions.EntityFrameworkCore namespace not found in .NET Core 2.2

asp.net-core c# entity-framework-core

Question

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.

He says to add the following to the .csproj file...

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

...and then add the following to the ConfigureServices method in Startup.cs...

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...

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. Information on resolving this warning is available at (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:

Type ApplicationDbContext could not be found.

I added a using for the namespace Microsoft.Extensions.EntityFrameworkCore, but that caused a compiler error saying it could not be found. I took the offer (from R# I think) to search Nuget for it, but it didn't come up with anything

Anyone any ideas? Other than downgrading to 2.0, I don't know how to proceed.

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.

1
2
4/1/2019 4:20:48 PM

Accepted Answer

The bad thing about ASP.NET Core books, so far that is, is that they become obsolete quite quickly.

This page explains that the <DotNetCliToolReference> references for dotnet ef commands are now part of the SDK, so those aren't needed anymore.

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. You have two options for fixing this:

  • Recreate the project using 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. Quite a big amount of tedious work.

3
4/1/2019 4:47:17 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow