No context type was found in the assembly

后端 未结 5 1127
失恋的感觉
失恋的感觉 2020-12-30 19:53

I\'m using .NET 4.0, MVC3, and EF5 with code first.

My solution is split up into three projects, with the dependencies as indicated:

Project.Web -> Project.B

相关标签:
5条回答
  • 2020-12-30 20:46

    I eventually found the answer in this question. Basically, in the Package Manager Console there's a "Default project" dropdown. You need to set this to the project that contains your EF context.

    0 讨论(0)
  • 2020-12-30 20:50

    Also happens if for some reason your class with the connection isn't in the project. So right click and 'add to project' sorts that out.

    0 讨论(0)
  • 2020-12-30 20:51

    For whom who made this mistake like I did:

    Your context class must inherits from DbContext, just like that:

    public class DirectorRequestContext : DbContext
    {
        public DbSet<DirectorRequest> DirectorRequests { get; set; }
    }
    
    0 讨论(0)
  • 2020-12-30 20:51

    None of the other answers above answered my question.

    I ended up ditching the Package Manager Console because it seemed to be using EF6 instead of EFCore...

    Luckily, you can run these Entity Framework commands from plain ol' PowerShell:

    C:\MyRepository\MyProject dotnet ef migrations add InitialDbCreation
    

    (Make sure you're in the project directory that contains the DbContext inheritance).

    And finally, I got the real errror:

    Your startup project 'FantasyFitness' doesn't reference Microsoft.EntityFrameworkCore.Design. 
    This package is required for the Entity Framework Core Tools to work. 
    Ensure your startup project is correct, install the package, and try again.
    

    So, I go to the "Manage NuGet Packages" on the project and install Microsoft.EntityFrameworkCore.Design, close all my IDE's that may be locking the files, and then it finally worked.

    Note that I didn't even have to run the Enable-Migration command to get this to work... it's magic.

    0 讨论(0)
  • 2020-12-30 20:56

    I found similar post: Enable Migrations with Context in Separate Assembly?

    Example:

    enable-migrations -ContextProjectName MyProject.DBContexts -contexttypename MyProject.DBContexts.MyContextName -Verbose
    
    0 讨论(0)
提交回复
热议问题