Quick Tip: Entity Framework DB Initialization with ASP.NET Providers

I have a database with the ASP.NET providers already installed…I don’t have them represented in my Object Model for Entity Framework 4.1 but I don’t want to put them on a separate Database from my main application data.

The solution was rather easy:

public class UpdateDatabaseSchemaStrategy:IDatabaseInitializer<KharaSoftIncipireWebContext>
{
  public void InitializeDatabase(KharaSoftIncipireWebContext context)
  {
    var script = ((IObjectContextAdapter) context).ObjectContext.CreateDatabaseScript();
    context.Database.ExecuteSqlCommand(script);
  }

I just asked the ObjectContext for the DBScript and call ExecuteSQLCommand using it. This is great for development where I’m rapidly pushing changes to the object model and want to see it in action. I just delete all the application tables and don’t have to worry about recreating the provider tables every round.

Published Saturday, June 11, 2011 12:20 PM by Mike Brown

Leave a Comment

(required) 
(required) 
(optional)
(required)