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

后端 未结 24 2011
野的像风
野的像风 2020-12-02 11:51

I have 4 projects :

Toombu.Entities : all models are there
Toombu.DataAccess: Mapping, Repository and ToombuContext
Toombu.Logique : Logic of my application         


        
相关标签:
24条回答
  • 2020-12-02 12:31

    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.

    0 讨论(0)
  • 2020-12-02 12:32

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

    0 讨论(0)
  • 2020-12-02 12:32

    Follow the below steps to resolve the issue

    Install-Package EntityFramework-IncludePrerelease
    

    or Install entity framework from Nuget Package Manager

    Restart visual studio

    After that I was getting "No context type was found in assembly"

    To resolve it - This "No context" that mean you need to create class in "Model" folder in your app with suffix like DbContext ... like this AppDbContext. There you need to include some library using System.Data.Entity;

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.Entity;
    
    
    namespace Oceans.Models
    {
        public class MyDbContext:DbContext
        {
            public MyDbContext()
            {
            }
        }
    }
    

    After that run the below command on Package Manager:

    Enable-Migrations -ProjectName <YourProjectName> -ContextTypeName <YourContextName>
    

    My Project Name is - MyFirstApp and AppDbContext is inside the Model Folder so path is like

    Enable-Migrations -StartUpProjectName MyFirstApp -ContextTypeName MyFirstApp.Models.AppDbContext
    
    0 讨论(0)
  • 2020-12-02 12:34

    In my case, the NuGet package "Microsoft.EntityFrameworkCore.Tools" was missing

    0 讨论(0)
  • 2020-12-02 12:36

    Adding a class which inherits DbContext resolved my problem:

    public class MyDbContext : DbContext { public MyDbContext() { } }
    
    0 讨论(0)
  • 2020-12-02 12:39

    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

    0 讨论(0)
提交回复
热议问题