Search code examples
asp.net-mvc-4entity-framework-5dbcontextn-tier-architecture

Asp.net MVC 4 How to enable Migrations from DbContext in another Project?


My Project has 3 tiers:

  • Project.Core: Entities and Repositories Interfaces.
  • Project.Data: DbContext, Repositories implementation and UnitOfWork implementation. Has reference of Core.
  • Project.UI: Views, Controllers, ViewModels. Has reference of Code and Data.

I'm using MVC 4 with Entity Framework 5 and Code First Approach.

So It's possible to enable POCOs Migrations on Project.UI?

I Tried run:

enable-migrations Project.Data.MyDbContext

But receive the message:

The context type 'Project.Data.MyDbContext' was not found in the assembly 'Project.UI'.


Solution

  • You need to make sure you are running enable-migrations on the project containing your DBContext so you cant run it on the UI project (otherwise it needs to be a relative path to the project from the currently selected one eg. ./project.data)

    enter image description here

    Set default project to Project.Data

    then run:

    enable-migrations