What is the best way to execute all required db migrations at application start with EF 4.3?
相关问题
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
- Bulk update SQL Server C#
- Entity Framework Code First Migration
A great description of the EF 4.3 configuration options can be found at EF 4.3 Configuration File Settings on the ADO.NET team blog. The very last section describes Database Initializers, including the new Code First
MigrateDatabaseToLatestVersion
initializer.Although Entity Framework—like so many other features of .NET 4.x—favors convention over configuration, this is one case where it might be very useful to set the
MigrateDatabaseToLatestVersion
database initializer through your application's config file rather than explicitly code it into your application.I needed to do it explicitly, because I use an uber-context for migration, a superset of the other migrations. The key bit is:
With a sprinkling of Elmah logging, I actually use this, called from Application_Start(). Pieces of it are stolen from others' ideas. I'm not positive that the thread-safety Interlocked part is necessary.
The best way should be using new
MigrateDatabaseToLatestVersion
initializer.