Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot enable migrations for Entity Framework in class library

I just got on board with EF 5 and am using their code-first migrations tool but I seem to get an error when I try to enable migrations.

I type Enable-Migrations into the package manager console and then it says

No classes deriving from DbContext found in the current project.
Edit the generated Configuration class to specify the context to enable migrations for.
Code First Migrations enabled for project MyApp.MvcUI.

It then creates a Migrations folder and a Configuration class in my MvcUI project. Thing is, my DbContext lives in a class library project called MyApp.Domain. It should be doing all that in that project and should have no problem finding my DbContext.

like image 333
Chev Avatar asked Mar 02 '12 02:03

Chev


People also ask

How do I enable migrations in .NET 5?

Go to Visual Studio "Tools -> NuGet Package Manager -> Package Manager Console". Then execute the following command. Enable-Migrations – ( We need to enable the migration, only then can we do the EF Code First Migration ). Add-Migration IntialDb (migration name) – ( Add a migration name and run the command ).


2 Answers

Oh wow, nevermind. I'm dumb.

In the Nuget package manager console there is a dropdown menu at the top labeled "Default Project:". Make sure you set that to the project you want to run the command against.

Hopefully this helps someone else avoid my embarrassing mistake.

like image 95
Chev Avatar answered Sep 18 '22 00:09

Chev


There are actually 3 ways to make Nuget commands run in a specific project:

  1. [Package Manager Console] Set the active project in the dropdown at the top of the console toolwindow
  2. [Package Manager Console] Look for a parameter to specify the project. For some cmdlets I've seen -ProjectName and some use -Project
  3. [Solution Explorer] Right-click the project you want, and use the graphical package manager window (Manage NuGet Packages...).
like image 27
Jimmy Avatar answered Sep 17 '22 00:09

Jimmy