简体   繁体   中英

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

However after successfully installing EntityFrameworkCore in the package manager console using the command:

Install-Package Microsoft.EntityFrameworkCore.SqlServer

then running a do.net restore successfully in the cmd, the project does not register EntityFrameworkCore. In the.csproj file you can see the line

<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. Any idea why this might be happening?

I solved this problem by:

(1) Right clicking the project in the Solution Explorer

(2) Clicking unload project

(3) Click edit the .csproj and check if there is a Package Reference to EF

(4) Right clicking the project again in the Solution Explorer

(5) Then clicked reload project

Now it recognizes EntityFrameworkCore and there are no more build errors

In Visual Code:

Ctrl + Shift + P => Restart 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:

  • Microsoft.EntityFrameworkCore.SqlServer
  • 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.

<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

If using VS Code, the solution that worked for me:

In the terminal type:

dotnet add package Microsoft.EntityFrameworkCore

After adding package, Ctrl + Shift + P -> Restart OmniSharp

  1. Go to Tools in your visual studio » NuGet Package Manager » Package Manager Console
  2. type->

    install-package microsoft.entityframeworkcore.sqlserver

In visual Studio, go to -> Project -> Manage NuGet Packages.. -> 在视觉

Select Microsoft Entity framework Core . The click on Add package on the bottom right corner button.

在此处输入图片说明

Clean up the .csproj file so if you see something like...

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM