Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No context type was found in the assembly

I'm using .NET 4.0, MVC3, and EF5 with code first.

My solution is split up into three projects, with the dependencies as indicated:

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

The Project.DAL layer contains my entity framework data context class and all my entities, but my startup project is Project.Web, so it contains my Web.config, connection strings, and the actual SQL compact database.

I'm trying to enable migrations so I can add a new table to my EF model without wiping the existing data. However, when I run "Enable-Migrations", I get

No context type was found in the assembly 'Project.Web'. 

If I set the startup project as Project.DAL, the error changes to

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

Does anyone know why this error is being caused or what I can do to fix it?

like image 942
Cavyn VonDeylen Avatar asked Oct 23 '12 13:10

Cavyn VonDeylen


People also ask

How do I enable-migrations in Visual Studio?

From the Tools menu, select NuGet Package Manager > Package Manager Console. The enable-migrations command creates a Migrations folder in the ContosoUniversity project, and it puts in that folder a Configuration. cs file that you can edit to configure Migrations.


2 Answers

I eventually found the answer in this question. Basically, in the Package Manager Console there's a "Default project" dropdown. You need to set this to the project that contains your EF context.

like image 98
Cavyn VonDeylen Avatar answered Sep 24 '22 11:09

Cavyn VonDeylen


I found similar post: Enable Migrations with Context in Separate Assembly?

Example:

enable-migrations -ContextProjectName MyProject.DBContexts -contexttypename MyProject.DBContexts.MyContextName -Verbose 
like image 26
d.danailov Avatar answered Sep 21 '22 11:09

d.danailov