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:44

Ensure you are using the same version of Entity Framework across all projects using the NuGet Package Manager.

Recent windows updates may have installed a newer version of Entity Framework into your active project.

Background: Around 16 Mar 2016, I started getting this error when trying to add migrations to a project where I had already enabled migrations and had successfully done migrations for.

I noticed that around March 10, a new stable version of Entity Framework 6 had been released.

If I specified the -ContextTypeName parameter in the enable-migrations command, I got an error indicating the migrations were already enabled.

Resolution:

1) Tools -> Nuget Package Manager -> Manage Nuget Packages for Solution

2) (Not sure if this step is necessary, but..) I updated my version of the Nuget Package Manager to the latest version. Also, after updating my version of Nuget Package Manager, I had to restart Visual Studio twice before the NuGet Command line would work properly.

3) Tools -> Nuget package Manager -> Manage Nuget Packages for Solution -> Search Installed packages -> Type Entity Framework

a. You may see more than one version of Entity Framework there.

b. Click Manage on each version of Entity Framework and ensure that your projects are using the SAME version of Entity Framework.

  • Uncheck the version of Entity Framework that you are not using and for the version of Entity Framework you ARE using make sure it is checked across your projects that need it.

Again, as noted in step 2, I had to restart visual studio twice to get the NuGet Package Manager Console to work properly after updating my version of the NuGet Package Manager. I got an error starting the console the first time, and "exception calling createinstancefrom with 8 arguments could not load file or assembly EntityFramework" when running the enable-migrations command the second time.

Restarting visual studio seemed to resolve those issues, however.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-10 14:45

Worked for me:

 UnInstall-Package EntityFramework 
  • Restart Visual Studio

Install-Package EntityFramework

  • Build project
查看更多
够拽才男人
4楼-- · 2019-01-10 14:46

This error getting because of the compiler not getting 'Context' class in your application. So, you can add it manually by Add --> Class and inherit it with 'DbContext' Class For Example :

public class MyDbContext : DbContext
    {
        public DbSet<Customer> Customer { get; set; }
        public MyDbContext()
        {
        }
    }
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-10 14:47

If anyone is still facing this problem. I solved it by using the following command:

Enable-Migrations -ProjectName <YOUR_PROJECT_NAME> -ContextTypeName <YOUR_CONTEXT_NAME>

Don't forget to use the full path to your context name.

查看更多
够拽才男人
6楼-- · 2019-01-10 14:48

You dbcontext is in Toombu.DataAccess So you should enable migrations in Toombu.DataAccess.

查看更多
Luminary・发光体
7楼-- · 2019-01-10 14:51

I have been getting this same problem. I have even tried above enable migrations even though I have already done. But it keeps giving same error. Then I had to use the force switch to get overcome this problem. I am sure this will help in someone else's case as well as its a possible work around.

After enabling migration with force, you should update your database (Make sure default project is set correctly). Otherwise you will get another problem like explicit migrations are pending.

Then just execute your add-migrations or any other commands, it should work.

Enable-Migrations -ProjectName <PROJECT_NAME> -ContextTypeName <FULL_CONTEXT_NAMESPACE.YOUR_CONTEXT_NAME> -force
查看更多
登录 后发表回答