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

NOr picture NOr · May 11, 2013 · Viewed 122.3k times · Source

I have 4 projects :

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.Web but i had this error :

No context type was found in the assembly

How can I enable migration ?

Answer

Jazimov picture Jazimov · Feb 14, 2014

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.

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:

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.

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.