EF 5 Enable-Migrations : No context type was found

2019-01-10 14:33发布

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 ?

16条回答
甜甜的少女心
2楼-- · 2019-01-10 14:55

Change the default project to data access

change the default project dropdown in the package manager console to data access and give enable migrations...

Thats all success

查看更多
甜甜的少女心
3楼-- · 2019-01-10 14:56

How to Update table and column in mvc using entity framework code first approach

1: tool > package manager console

2: select current project where context class exist

3: Enable migration using following command PM > enable-migrations

4: Add migration folder name using following command PM > add-migration MyMigrationName

4: Now update database following command PM > update-database

查看更多
冷血范
4楼-- · 2019-01-10 14:58

I had to do a combination of two of the above comments.

Both Setting the Default Project within the Package Manager Console, and also Abhinandan comments of adding the -ContextTypeName variable to my full command. So my command was as follows..

Enable-Migrations -StartUpProjectName RapidDeploy -ContextTypeName RapidDeploy.Models.BloggingContext -Verbose

My Settings::

  • ProjectName - RapidDeploy
  • BloggingContext (Class Containing DbContext, file is within Models folder of Main Project)
查看更多
Juvenile、少年°
5楼-- · 2019-01-10 14:59

use -ProjectName option in Package Manager Console:

Enable-Migrations -ProjectName Toombu.DataAccess -StartUpProjectName Toombu.Web -Verbose
查看更多
闹够了就滚
6楼-- · 2019-01-10 14:59

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.

查看更多
We Are One
7楼-- · 2019-01-10 15:00

Thanks for the suggestions, I solved the problem by combining all the solutions here. At first I created the DbContext Model:

 public class MyDbContext: DbContext
    {
        public MyDbContext()
        {
        }
    }

After creating the dbcontext class, I ran the enable-migration command with the project Name: enable-migrations -ProjectName YourProjectName

查看更多
登录 后发表回答