Recreate tables when a Code First class changes

If you want to automatically recreate the database if your Code First model changes in an asp.net application, add the following code to the Application_Start in global.asax.cs. Call Database.SetInitializer for your DbContext class.

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    Database.SetInitializer<TwitterDbContext>(
        new DropCreateDatabaseIfModelChanges<TwitterDbContext>());
}  More...

Simple Entity Framework and Code First example

I played around with a feature of Entity Framework; Code First. The concept is creating a data model from POCO – the Plain Old CLR Objects we use and love. Before you can do this you need to install the Entity Framework in your projects (preferably with NuGet) Just for the sake of simplicity, I created basic version of a Twitter Account data structure with Code First in a standard asp.net website. More…