在程序集中找不到上下文类型

Cav*_*len 33 asp.net entity-framework connection-string data-access-layer entity-framework-5

我首先使用.NET 4.0,MVC3和EF5代码.

我的解决方案分为三个项目,依赖项如下所示:

Project.Web - > Project.BLL - > Project.DAL

Project.DAL层包含我的实体框架数据上下文类和我的所有实体,但我的启动项目是Project.Web,因此它包含我的Web.config,连接字符串和实际的SQL压缩数据库.

我正在尝试启用迁移,因此我可以在不擦除现有数据的情况下向EF模型添加新表.但是,当我运行"启用 - 迁移"时,我得到了

No context type was found in the assembly 'Project.Web'.
Run Code Online (Sandbox Code Playgroud)

如果我将启动项目设置为Project.DAL,则错误更改为

Could not load assembly 'Project.Web'. (If you are using Code First Migrations inside Visual Studio this can happen if the startUp project for your solution does not reference the project that contains your migrations. You can either change the startUp project for your solution or use the -StartUpProjectName parameter.)
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么会出现这个错误或者我可以做些什么来修复它?

Cav*_*len 83

我最终在这个问题上找到了答案.基本上,在软件包管理器控制台中有一个"默认项目"下拉列表.您需要将其设置为包含EF上下文的项目.

  • 一定要放大你的包管理器控制台窗口,或者它可能在窗口工具栏的右侧,你会像我一样想念它. (10认同)