Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
888 views
in Technique[技术] by (71.8m points)

azure - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...