Luke Warren Dev
Software Development Tips and Stories When I feel Like It

Enabling Migrations for Entity Framework with your DBContext in a Different Project

OK, so code first migrations in EF is flipping awesome.

,
Categories: Entity Framework
Tags: EF , Entity Framework , Powershell

OK, so code first migrations in EF is flipping awesome. That being said, I find keeping my data access layer in the same project as my MVC website... not awesome. It makes code sharing impossible when you have a few different presentation layers as well as making your MVC project bloated and ugly

Typically, when building an MVC website my project structure looks like this:

typical_project_structure

 

You will notice that my EF DBContext (DebonairsContext) is in a different directory to my MVC folder (where my migrations are). This makes the simple enable-migrations command not work. To enable migrations for a solution of this structure, all you need is to use the following package manager command (the one shown is the one I used for the solution above):

 

enable-migrations -ContextProjectName Debonairs.Data -StartUpProjectName Debonairs.Dashboard -ContextTypeName Debonairs.Data.DebonairsContext -ProjectName Debonairs.Dashboard

Note - The above will only work for EF 6 and up. Over and out


comments powered by Disqus