tag:support.appharbor.com,2010-11-23:/discussions/problems/31287-entity-framework-code-first-migrationAppHarbor: Discussion 2013-06-22T21:31:21Ztag:support.appharbor.com,2010-11-23:Comment/274438522013-06-22T06:00:59Z2013-06-22T06:07:30ZEntity Framework Code First Migration<div><p>Hi Ido,</p>
<ol>
<li>Actually <code>Application_Start</code> is 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 an
<code>HttpApplication</code>). 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.<br></li>
<li>Yes - when you make changes to the database make sure that
it'll supports both the old and the new version when you're
deploying. A good way to accomplish that is to avoid destructive
migrations, for instance migrations that drop columns that are
being used by the application. This is usually easy to accomplish
simply by making a couple of deployments that each perform a part
of the migration.</li>
</ol>
<p>We've written a couple of blog posts about it that covers a lot
more stuff. You can find them here:</p>
<ul>
<li><a href=
"http://blog.appharbor.com/2012/04/24/automatic-migrations-with-entity-framework-4-3">
Automatic migrations with Entity Framework 4.3</a></li>
<li><a href=
"http://blog.appharbor.com/2012/10/31/repository-pattern-and-convention-based-entity-framework-code-first">
Repository pattern and convention-based Entity Framework code
first</a></li>
</ul>
<p>Best,<br>
Rune</p></div>runetag:support.appharbor.com,2010-11-23:Comment/274438522013-06-22T10:39:39Z2013-06-22T10:39:39ZEntity Framework Code First Migration<div><p>Can you please explain how prewarm works, is that means sending
HTTP request to the / URL or something else?</p></div>idorantag:support.appharbor.com,2010-11-23:Comment/274438522013-06-22T16:36:56Z2013-06-22T16:36:56ZEntity Framework Code First Migration<div><p>Hi Ido,</p>
<p>Yes there <a href=
"http://blog.appharbor.com/2012/09/04/web-worker-pre-warming">are
more details in the this blog post about prewarming</a>. We should
probably make that into a knowledge base article coming to thing of
it.</p>
<p>Best,<br>
Rune</p></div>runetag:support.appharbor.com,2010-11-23:Comment/274438522013-06-22T20:53:37Z2013-06-22T20:53:37ZEntity Framework Code First Migration<div><p>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.</p>
<p>How can I be sure that the migration run only once?</p></div>idorantag:support.appharbor.com,2010-11-23:Comment/274438522013-06-22T21:31:19Z2013-06-22T21:31:19ZEntity Framework Code First Migration<div><p>Hi Ido,</p>
<p>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.</p>
<p>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 <a href=
"http://stackoverflow.com/questions/10822618/confusion-over-ef-auto-migrations-and-seeding-seeding-every-program-start">
seeding data in this Stack Overflow post</a>.</p>
<p>I can't talk for Entity Framework, but we're using Fluent
NHibernate's <code>SchemaExport</code> method (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.</p>
<p>Best,<br>
Rune</p></div>rune