简体   繁体   English

错误 - 当我尝试在 .net core 2.2 中启用迁移时,项目上未安装实体框架包

[英]Error -The Entity Framework package is not installed on project when I try to enable migration in .net core 2.2

I am working on a .NET Core 2.2 project.我正在处理一个 .NET Core 2.2 项目。 And I installed the Entity Framework Core nuget package.我安装了 Entity Framework Core nuget 包。

But when I try to enable migrations, got error但是当我尝试启用迁移时,出现错误

The EntityFramework package is not installed on project EntityFramework 包未安装在项目上

I found one solution for that,我找到了一个解决方案,

One possibility for this error - In the Package Manager Console, there is a dropdown for 'Default Project'.此错误的一种可能性 - 在包管理器控制台中,有一个“默认项目”下拉菜单。 If this is set incorrectly, you will get The EntityFramework package is not installed on project 'x'.如果设置不正确,您将得到 EntityFramework 包未安装在项目“x”上。 Change the dropdown to your EF project and all is good again.将下拉列表更改为您的 EF 项目,一切都恢复正常。

But the selected project is correct.但是选择的项目是正确的。

So I checked the module in package manager console with command,所以我用命令检查了包管理器控制台中的模块,

PM> Get-Module EntityFramework*

ModuleType Version    Name                                ExportedCommands                                                 
---------- -------    ----                                ----------------                                                 
Script     6.0.0.0    EntityFramework                     {Add-EFDefaultConnectionFactory, Add- 
EFProvider, Add-Migration...
Script     2.2.6      EntityFrameworkCore                 {Add-Migration, Drop-Database, Enable- 
Migrations, Get-DbContex...*

So I think there are two Entity Framework versions found in my class library.所以我认为在我的类库中发现了两个实体框架版本。 But when I checked in manage nuget package option, there is Entity Framework not found , only Entity Framework Core is present.但是当我签入管理 nuget 包选项时,找不到 Entity Framework ,只有 Entity Framework Core 存在。

在此处输入图片说明

How to solve this issue?如何解决这个问题?

If your DbContext is in a different, bespoke class library and not in your startup project, chances are you need to specify this project in your EF commands:如果您的DbContext位于不同的定制类库中,而不是在您的启动项目中,则您可能需要在 EF 命令中指定此项目:

With powershell that would be使用 powershell 那将是

Add-migration NameOfMigration -Project Namespace.ToContextProject

And with the dotnet cli并使用 dotnet cli

dotnet ef migrations add NameOfMigration --project relativePath/ToContextProject
dotnet ef migrations add InitialCreate -p  relativePath/ToContextProject

I had a similar issue where upon running Get-Module EntityFramework* I had 2 references, 1 for EntiryFramework 6.0 and the second on EntityFrameworkCore.我有一个类似的问题,在运行 Get-Module EntityFramework* 时,我有 2 个引用,1 个用于 EntiryFramework 6.0,第二个用于 EntityFrameworkCore。

I never installed EntityFramework6.我从未安装过 EntityFramework6。 To get past this, however, I had to qualify the framework that I wanted to add the migration through as well as update the database.然而,为了解决这个问题,我必须限定我想要添加迁移以及更新数据库的框架。

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

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