Database Initializers and EF Code First - Database.SetInitializer

I have been using EF Code First with ASP.NET MVC 3 in a number of tutorials:

You can find those tutorials in my large list of ASP.NET MVC 3 Tutorials as well.

In addition to the features mentioned in the tutorials above, another feature worth mentioning are Database Initializers. Database Initializers allow you to run custom logic the first time a DbContext class is first used in your application. In the example below I will use it to populate the database with seed data.

 

Database Initializers and Database.SetInitializer using EF Code First

I am using the same classes used in the previous 3 tutorials as well as the MvcScaffolding Package to generate my ASP.NET MVC 3 Application to show a list of books. This time, however, I created a Database Initializer, called LibraryInitializer, that

  1. Drops and creates the SQL Compact Database if the model has changed.
  2. Adds two books to the database upon creation.

All of this is taken care of by having LibraryInitializer derive from DropCreateDatabaseIfModelChanges and overriding the Seed Method to initially populate my database on creation. Here are the model, DbContext, and database initializer classes used by the Entity Framework at a glance:

 

Database Initializers Database.SetInitializer in EF Code First

 

I need to tell Entity Framework about this database initializer as well as that I want to use SQL CE 4 as my database. An easy enough place to do this is in Application_Start of our ASP.NET MVC Web Application. Here is where I do the following:

  1. Database.SetInitializer(new LibraryInitializer());
  2. Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");

 

Database.SetInitializer and Database.DefaultConnectionFactory

 

With this done, I can now run the MvcScaffolding Nuget Package for a bit of code generation in ASP.NET MVC 3. I use MvcScaffolding to create the BooksController and Views to handle CRUD Actions for creating, reading, updating, and deleting books.

The ASP.NET MVC 3 application is similar to those discussed in the previous EF Code First Tutorials, except now the LibraryInitializer has automatically populated the two books on creation of the SQL Compact 4 Database. The LibraryInitializer will also drop, recreate, and re-populate the database if the Book Model changes.

 

ASP.NET MVC Web Application using EF Code First

 

Conclusion

EF Code First has some really nice features. Use it with the new SQL Compact 4 Database and MvcScaffolding Nuget Package and you have some pretty neat tools to help build ASP.NET MVC Web Applications.

 

David Hayden

 

Related: ASP.NET MVC Resources

 

posted on Wednesday, April 27, 2011 7:44 AM

Main

David Hayden Google +

David Hayden Twitter

Health & Fitness

JavaScript Patterns Book Review

HTML 5 and CSS3 - Develop with Tomorrow's Standards Today

Professional ASP.NET Design Patterns Book Review

Beginning Mac Programming Book Review

C# in Depth Book Review

ASP.NET MVC

Orchard CMS

Categories