Entity Framework Code First Migration
Hi,
In my ASP MVC application I use Entity Framework 4.4 Code First Migration.
1. I need to find good place to run the migrations. Until now I use the Application_Start handler but in your cluster and dynamic environment I saw that it doesn't always consistant place that run every time a new version is deployed. Maybe I should add a controller that I should call after deploying that will do the actual migration, this way I'll have complete control over when the migration runs?
2. When deploying a new version there is a time window in which both the previous and and the current versions are both accessible. Do you have any suggestion on how to minimize the chance that the new version database changes will prevent the previous version from working? I know it's a broad question and the answer is very case-specific but maybe you have like patterns and anti-patterns you already saw.
Thank you,
Ido.
Discussions are closed to public comments.
If you need help with AppHarbor please
start a new discussion.
Keyboard shortcuts
Generic
| ? | Show this help |
|---|---|
| ESC | Blurs the current field |
Comment Form
| r | Focus the comment reply box |
|---|---|
| ^ + ↩ | Submit the comment |
You can use Command ⌘ instead of Control ^ on Mac
Support Staff 1 Posted by rune on 22 Jun, 2013 06:00 AM
Hi Ido,
Application_Startis a good place to run your migrations, because it's guaranteed to only run once when the application is started (unlike for instance the constructor of anHttpApplication). When you deploy a new version it'll run once for each web worker. I'd highly recommend this approach over calling a controller action, since you're guaranteed that the request will be served with the migrated database (which is good since your code depends on it). It's also an advantage because we prewarm the application, which means the migration will run before requests are routed to the app.We've written a couple of blog posts about it that covers a lot more stuff. You can find them here:
Best,
Rune
rune closed this discussion on 22 Jun, 2013 06:00 AM.
idoran re-opened this discussion on 22 Jun, 2013 10:39 AM
2 Posted by idoran on 22 Jun, 2013 10:39 AM
Can you please explain how prewarm works, is that means sending HTTP request to the / URL or something else?
Support Staff 3 Posted by rune on 22 Jun, 2013 04:36 PM
Hi Ido,
Yes there are more details in the this blog post about prewarming. We should probably make that into a knowledge base article coming to thing of it.
Best,
Rune
rune closed this discussion on 22 Jun, 2013 04:36 PM.
idoran re-opened this discussion on 22 Jun, 2013 08:53 PM
4 Posted by idoran on 22 Jun, 2013 08:53 PM
Thank you. One thing I still thinking about is how to make sure each migrations runs only once. What happen if after I deploy a new version of my app two or more servers start to run it, both might check if they have out-standing migration and both might start to run the migration in which case the result is unknown. Maybe they both succeed, maybe they both fail, maybe just one fail.
How can I be sure that the migration run only once?
Support Staff 5 Posted by rune on 22 Jun, 2013 09:31 PM
Hi Ido,
This seems like a quite general issue - you might have more luck finding an answer for your use case by searching online or by asking on Stack Overflow. There seems to be plenty of resources on the topic. This is a challenge that all applications that run on more than a single instance potentially face when using migrations as part of the startup cycle.
However it's essentially not an issue if you don't perform destructive migrations, and as long as you don't add data during the migration process. You can read more about seeding data in this Stack Overflow post.
I can't talk for Entity Framework, but we're using Fluent NHibernate's
SchemaExportmethod (as you can see in the code samples I linked to before). This runs every time we deploy, on every instance, and we haven't seen any issues related to that.Best,
Rune
rune closed this discussion on 22 Jun, 2013 09:31 PM.