C# EF 5 Enable-Migrations:在程序集中找不到上下文类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16497938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:09:50  来源:igfitidea点击:

EF 5 Enable-Migrations : No context type was found in the assembly

c#entity-frameworkasp.net-mvc-4ef-migrations

提问by NOr

I have 4 projects :

我有 4 个项目:

Toombu.Entities : all models are there
Toombu.DataAccess: Mapping, Repository and ToombuContext
Toombu.Logique : Logic of my application
Toombu.Web : MVC 4 application. With all others DLL.

I tried to enable migration in Toombu.Webbut i had this error :

我试图在Toombu.Web 中启用迁移,但出现此错误:

No context type was found in the assembly

How can I enable migration ?

如何启用迁移?

采纳答案by hVostt

use -ProjectName option in Package Manager Console:

在包管理器控制台中使用 -ProjectName 选项:

Enable-Migrations -ProjectName Toombu.DataAccess -StartUpProjectName Toombu.Web -Verbose

回答by Kirill Bestemyanov

You dbcontext is in Toombu.DataAccessSo you should enable migrations in Toombu.DataAccess.

你 dbcontext 在Toombu.DataAccess所以你应该在Toombu.DataAccess.

回答by Jazimov

I am surprised that no one mentioned the obvious answer to this question: Entity Framework requires a context before enable-migrations will work. The error message the OP posted suggests that no context was found. Sure, it could be because the package manager console doesn't "see" the context--in which case the accepted answer is a possible solution (another solution is one I suggest, below). But a context must exist in the current project (assembly) before any other solutions will work.

我很惊讶没有人提到这个问题的明显答案:实体框架需要一个上下文才能启用迁移工作。OP 发布的错误消息表明未找到上下文。当然,这可能是因为包管理器控制台没有“看到”上下文——在这种情况下,接受的答案是一个可能的解决方案(另一个解决方案是我在下面建议的一个解决方案)。但是在任何其他解决方案起作用之前,当前项目(程序集)中必须存在上下文。

What does it mean to have a context? It means that there must exist a class in your project that inherits from DbContext (in System.Data.Entity). Here is an example:

有上下文是什么意思?这意味着您的项目中必须存在一个继承自 DbContext(在 System.Data.Entity 中)的类。下面是一个例子:

public class MyDbContext : DbContext
{
    public MyDbContext()
    {
    }
}

Be sure you use

确保您使用

using System.Data.Entity;

before the code above has access to the DbContext class and that you have used NuGet to get Entity Framework 4.1 or later for the current project.

在上面的代码可以访问 DbContext 类并且您已经使用 NuGet 获取当前项目的实体框架 4.1 或更高版本之前。

If all along you had a context but the Package Manager Console just doesn't "see" it: In Visual Studio 2013 you don't have to use the -ProjectName switch. Instead, go to the Package Manager Console (it's available in the View | Other Windows list), and look at the two dropdowns that appear at the top of the Package Manager Console dockable window. The first dropdown is for Package Source; the second is for Default Project. If you dropdown the Default Project and select a project in your solution then whatever commands you issue in the Package Manager console will be executed against the selected project.

如果您一直都有上下文,但包管理器控制台没有“看到”它:在 Visual Studio 2013 中,您不必使用 -ProjectName 开关。相反,转到包管理器控制台(它在视图 | 其他 Windows 列表中可用),然后查看出现在包管理器控制台可停靠窗口顶部的两个下拉菜单。第一个下拉菜单是包源;第二个是默认项目。如果您下拉默认项目并在您的解决方案中选择一个项目,那么您在包管理器控制台中发出的任何命令都将针对所选项目执行。

回答by ohmprakashinfo

Change the default project to data access

将默认项目更改为数据访问

change the default project dropdown in the package manager console to data accessand give enable migrations...

将包管理器控制台中的默认项目下拉列表更改为数据访问并启用迁移...

Thats all success

这就是所有的成功

回答by Sadjad Khazaie

Change the default project and choose the startup project from dropdown: enter image description here

更改默认项目并从下拉列表中选择启动项目: 在此处输入图片说明

回答by Abhinandan

If anyone is still facing this problem. I solved it by using the following command:

如果有人仍然面临这个问题。我使用以下命令解决了它:

Enable-Migrations -ProjectName <YOUR_PROJECT_NAME> -ContextTypeName <YOUR_CONTEXT_NAME>

Don't forget to use the full path to your context name.

不要忘记使用上下文名称的完整路径。

回答by WWC

Ensure you are using the same version of Entity Framework across all projects using the NuGet Package Manager.

确保使用 NuGet 包管理器在所有项目中使用相同版本的实体框架。

Recent windows updates may have installed a newer version of Entity Framework into your active project.

最近的 Windows 更新可能已将较新版本的实体框架安装到您的活动项目中。

Background: Around 16 Mar 2016, I started getting this error when trying to add migrations to a project where I had already enabled migrations and had successfully done migrations for.

背景:大约在 2016 年 3 月 16 日,当我尝试将迁移添加到我已经启用迁移并成功完成迁移的项目时,我开始收到此错误。

I noticed that around March 10, a new stable version of Entity Framework 6 had been released.

我注意到 3 月 10 日左右,实体框架 6 的新稳定版本已经发布。

If I specified the -ContextTypeName parameter in the enable-migrations command, I got an error indicating the migrations were already enabled.

如果我在 enable-migrations 命令中指定了 -ContextTypeName 参数,则会收到一条错误消息,表明迁移已启用。

Resolution:

解析度:

1) Tools -> Nuget Package Manager -> Manage Nuget Packages for Solution

1) 工具 -> Nuget 包管理器 -> 管理解决方案的 Nuget 包

2) (Not sure if this step is necessary, but..) I updated my version of the Nuget Package Manager to the latest version. Also, after updating my version of Nuget Package Manager, I had to restart Visual Studio twice before the NuGet Command line would work properly.

2)(不确定这一步是否必要,但是..)我将 Nuget 包管理器的版本更新到了最新版本。此外,在更新我的 Nuget 包管理器版本后,我必须重新启动 Visual Studio 两次,然后 NuGet 命令行才能正常工作。

3) Tools -> Nuget package Manager -> Manage Nuget Packages for Solution -> Search Installed packages -> Type Entity Framework

3) 工具 -> Nuget 包管理器 -> 管理解决方案的 Nuget 包 -> 搜索已安装的包 -> 类型实体框架

a. You may see more than one version of Entity Framework there.

一种。您可能会在那里看到多个版本的实体框架。

b. Click Manage on each version of Entity Framework and ensure that your projects are using the SAME version of Entity Framework.

湾 单击每个版本的实体框架上的管理,并确保您的项目使用相同版本的实体框架。

  • Uncheck the version of Entity Framework that you are not using and for the version of Entity Framework you ARE using make sure it is checked across your projects that need it.
  • 取消选中您未使用的实体框架版本,对于您正在使用的实体框架版本,请确保在需要它的项目中进行检查。

Again, as noted in step 2, I had to restart visual studio twice to get the NuGet Package Manager Console to work properly after updating my version of the NuGet Package Manager. I got an error starting the console the first time, and "exception calling createinstancefrom with 8 arguments could not load file or assembly EntityFramework" when running the enable-migrations command the second time.

同样,如步骤 2 中所述,在更新我的 NuGet 包管理器版本后,我必须重新启动 Visual Studio 两次才能使 NuGet 包管理器控制台正常工作。我第一次启动控制台时出错,第二次运行 enable-migrations 命令时“异常调用 createinstancefrom 带有 8 个参数无法加载文件或程序集 EntityFramework”。

Restarting visual studio seemed to resolve those issues, however.

然而,重新启动 Visual Studio 似乎解决了这些问题。

回答by ekgcorp

I had to do a combination of two of the above comments.

我必须结合上述两个评论。

Both Setting the Default Project within the Package Manager Console, and also Abhinandan comments of adding the -ContextTypeName variable to my full command. So my command was as follows..

在包管理器控制台中设置默认项目,以及将 -ContextTypeName 变量添加到我的完整命令的 Abhinandan 评论。所以我的命令如下..

Enable-Migrations -StartUpProjectName RapidDeploy -ContextTypeName RapidDeploy.Models.BloggingContext -Verbose

My Settings::

我的设置::

  • ProjectName - RapidDeploy
  • BloggingContext (Class Containing DbContext, file is within Models folder of Main Project)
  • 项目名称 - RapidDeploy
  • BloggingContext(包含 DbContext 的类,文件位于主项目的 Models 文件夹中)

回答by Riyaz Hameed

I have been getting this same problem. I have even tried above enable migrations even though I have already done. But it keeps giving same error. Then I had to use the forceswitch to get overcome this problem. I am sure this will help in someone else's case as well as its a possible work around.

我一直在遇到同样的问题。我什至尝试过上面启用迁移,即使我已经完成了。但它不断给出同样的错误。然后我不得不使用强制开关来克服这个问题。我相信这将有助于其他人的情况以及可能的解决方法。

After enabling migration with force, you should update your database (Make sure default project is set correctly). Otherwise you will get another problem like explicit migrations are pending.

启用强制迁移后,您应该更新数据库(确保正确设置默认项目)。否则你会遇到另一个问题,比如显式迁移正在等待中。

Then just execute your add-migrations or any other commands, it should work.

然后只需执行您的 add-migrations 或任何其他命令,它应该可以工作。

Enable-Migrations -ProjectName <PROJECT_NAME> -ContextTypeName <FULL_CONTEXT_NAMESPACE.YOUR_CONTEXT_NAME> -force

回答by befree2j

Thanks for the suggestions, I solved the problem by combining all the solutions here. At first I created the DbContext Model:

感谢您的建议,我通过结合此处的所有解决方案解决了问题。起初我创建了 DbContext 模型:

 public class MyDbContext: DbContext
    {
        public MyDbContext()
        {
        }
    }

After creating the dbcontext class, I ran the enable-migration command with the project Name: enable-migrations -ProjectName YourProjectName

创建 dbcontext 类后,我使用项目名称运行 enable-migration 命令:enable-migrations -ProjectName YourProjectName