RSS

Code First Migrations StartUp Project Does Not Reference The Project Contains Migrations

11 Jun

Could not load assembly ‘CFMigrationsSyntax.DataLayer’. (If you are using Code First Migrations inside Visual Studio this can happen if the startUp project for your solution does not reference the project that contains your migrations. You can either change the startUp project for your solution or use the -StartUpProjectName parameter.)

Depend on your solution and projects setup in Visual Studio, sometime Entity Framework Code First commands don’t run as expected.

By default, when Code First migration commands run, it will pick the default project set on your Package Manager Console:

code-first-migrations-startup-project-does-not-reference-the-project-contains-migrations-1

But most of the time, we have separate projects in Visual Studio solutions for presentation, business and data layers. EF Code First migration is enabled on the project where your EF Context lives, data layer, while the startup project for the solution is the presentation layer’s project. And since the startup project doesn’t have reference to data layer (where EF is), Package Manager Console will always throw error when running Code First migration commands.

Here’s my typical projects setup:

code-first-migrations-startup-project-does-not-reference-the-project-contains-migrations-2

MainConsole is the presentation and reference to the business layer, CFMigrationsSyntax.BusinessLayer project. In turn, CFMigrationsSyntax.BusinessLayer reference to the data layer, CFMigrationsSyntax.DataLayer.

To run EF Code First commands againts the data layer project, CFMigrationsSyntax.DataLayer, first change the default project in Package Manager Console to your EF project and then add the following parameter to the Code First migration commands:

-StartUpProjectName <EF_project>

In my case:

PM> Enable-Migrations -StartUpProjectName CFMigrationsSyntax.DataLayer
Advertisements
 
1 Comment

Posted by on June 11, 2013 in General

 

One response to “Code First Migrations StartUp Project Does Not Reference The Project Contains Migrations

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: