No context type was found in the assembly
Asked Answered
D

6

33

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?

Disposal answered 23/10, 2012 at 13:48 Comment(0)
D
82

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.

Disposal answered 23/10, 2012 at 15:39 Comment(2)
Be sure to enlarge your Package Manager Console window, or it might be off the right side of the toolbar of the window, and you'll miss it like I did.Deejay
Is there any way to set this up in a configuration somewhere? I'm trying to setup automatic migrations when I publish to azure, but it seems to fail because it runs the update-database command on the startup project instead of the DALStriker
J
4

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

Example:

enable-migrations -ContextProjectName MyProject.DBContexts -contexttypename MyProject.DBContexts.MyContextName -Verbose
Jail answered 2/11, 2015 at 21:47 Comment(0)
S
1

For whom who made this mistake like I did:

Your context class must inherits from DbContext, just like that:

public class DirectorRequestContext : DbContext
{
    public DbSet<DirectorRequest> DirectorRequests { get; set; }
}
Stays answered 28/11, 2016 at 13:15 Comment(0)
C
1

None of the other answers above answered my question.

I ended up ditching the Package Manager Console because it seemed to be using EF6 instead of EFCore...

Luckily, you can run these Entity Framework commands from plain ol' PowerShell:

C:\MyRepository\MyProject dotnet ef migrations add InitialDbCreation

(Make sure you're in the project directory that contains the DbContext inheritance).

And finally, I got the real errror:

Your startup project 'FantasyFitness' doesn't reference Microsoft.EntityFrameworkCore.Design. 
This package is required for the Entity Framework Core Tools to work. 
Ensure your startup project is correct, install the package, and try again.

So, I go to the "Manage NuGet Packages" on the project and install Microsoft.EntityFrameworkCore.Design, close all my IDE's that may be locking the files, and then it finally worked.

Note that I didn't even have to run the Enable-Migration command to get this to work... it's magic.

Corkwood answered 8/2, 2020 at 21:43 Comment(0)
S
0

Also happens if for some reason your class with the connection isn't in the project. So right click and 'add to project' sorts that out.

Sillabub answered 22/9, 2016 at 13:54 Comment(0)
A
0

My issue is that I had EF6 and EntityFrameworkCore both installed, I had to resolve this issue by specifying in the Package Manager Console:

EntityFrameworkCore\Add-Migration
Armagh answered 3/5, 2024 at 21:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.