Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publish Entity-Framework Code-First Migrations with no Context in the startup project

I am building a solution with the following Projects:

  • Main.Data - Class Library project
  • Main.API - Asp.NET MVC WebApi - references Main.Data
  • Main - Asp.NET MVC 4 web application - references Main.API

I have a MyContext : DbContext class located inside Main.Data project. I have also successfully issues enable-migrationsconsole command on Main.Data project, and I am successfully using LocalDB as an SQL server for my data and for upgrade-database migrations.

The problem starts when I am trying to publish Main project to Windows Azure website.

The Publish Profile that is automatically created using Import from a Windows Azure web site does not seems to recognize that I am using Entity Framework Code First solution, and so I can't enable Execute Code First Migrations as I would like to.
Instead, I can only enable Update database scripts.

I am using Visual Studio 2012 with Entity Framework 5.0.0 (Since the beginning of the project).

Just to verify, I have tried to add a temporary MyContext class inside the Main Project and to enable-migrations on the Main project, and after that my Publish Profile automatically detected Entity Framework Code-First.
That is, of-course, not a solution (or is it?)

Here are some relevant threads:

  • This is the base learning tutorial.
  • This explains deployment options, but no troubleshooting.
  • This actually gives a few ideas to try, but all seems unnatural.

I am looking for a clean stable solution. Do I have to put my Context class inside the Main Project?

Thanks in advance

like image 964
Liel Avatar asked May 14 '13 12:05

Liel


People also ask

How do I code my first migration to an existing database?

Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.

How do I move data into code first entity framework?

Step 1 − Before running the application you need to enable migration. Step 2 − Open Package Manager Console from Tools → NuGet Package Manger → Package Manger Console. Step 3 − Migration is already enabled, now add migration in your application by executing the following command.


1 Answers

I can now enable Execute Code First Migrations when I create a publish profile.
Here is what I did to achieve it:

  1. Inside Main/Web.config I changed the name of the connection string to the FQN of the context class: Main.Data.MyContext.
  2. Add a reference from Main project to Main.Data Project (which was not needed until now).

This does the job for me.
If anyone got a better or more educating answer, I would be happy to hear it.

like image 92
Liel Avatar answered Jan 01 '23 18:01

Liel