Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 950
NOr Avatar asked May 11 '13 14:05

NOr


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

like image 163
Jazimov Avatar answered Sep 18 '22 12:09

Jazimov


use -ProjectName option in Package Manager Console:

Enable-Migrations -ProjectName Toombu.DataAccess -StartUpProjectName Toombu.Web -Verbose 
like image 25
hVostt Avatar answered Sep 20 '22 12:09

hVostt