Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define Entity Framework SetInitializer in web.config

Right now, in development I have the following code in the Global.asax.cs file that calls the Entity Framework (v4.1) SetInitializer with my SeedSampleData method. This all works perfectly.

However, I would like to store the SetInitializer "strategy" parameter through a web.config setting so that I can create a deployement script that will automatically set it to new System.Data.Entity.CreateDatabaseIfNotExists<EfDbContext>() instead of my seed method during production deployment.

The reason for wanting to move this to the web.config is that when I roll out a new deployment to the production server I want to make sure that I don't accidentally leave my seed initializer in the code.

protected void Application_Start()
{
  //TODO: Figure out how to move the following lines to web.config and have a deployment script modify it when going to production.

  //This line is for production
  //System.Data.Entity.Database.SetInitializer(new System.Data.Entity.CreateDatabaseIfNotExists<EfDbContext>());

  //This line is for development
  System.Data.Entity.Database.SetInitializer(new Domain.Concrete.SeedSampleData());

  //... Remainder of Application_Start calls here...
}
like image 956
bigmac Avatar asked Mar 12 '12 20:03

bigmac


People also ask

What is database Setinitializer?

Sets the database initializer to use for the given context type. The database initializer is called when a the given DbContext type is used to access a database for the first time. The default strategy for Code First contexts is an instance of CreateDatabaseIfNotExists<TContext>. C# Copy.

How do I Code first in Entity Framework?

Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…

What is OnModelCreating in Entity Framework?

The DbContext class has a method called OnModelCreating that takes an instance of ModelBuilder as a parameter. This method is called by the framework when your context is first created to build the model and its mappings in memory.


1 Answers

If you update to EF 4.3 (which is a good idea anyway), then you can use something like this in your web config:

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>

  <entityFramework>
    <contexts>
      <context type=" Blogging.BlogContext, MyAssembly">
        <databaseInitializer type="Blogging.MyCustomBlogInitializer, MyAssembly" />
      </context>
    </contexts>
  </entityFramework>
</configuration>

Rowan wrote about it in detail here: http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-configuration-file-settings.aspx

If you really want to keep using 4.1, then there is an older syntax that you can use instead. I wrote about it here: http://blog.oneunicorn.com/2011/03/31/configuring-database-initializers-in-a-config-file/

like image 64
Arthur Vickers Avatar answered Sep 18 '22 15:09

Arthur Vickers