Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
994 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...