In web.config, define Entity Framework SetInitializer.

entity-framework web-config

Accepted Answer

You may use something like this in your web configuration if you upgrade to EF 4.3 (which is a good idea in any case):

<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>

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

There is an older syntax you may use in its place if you truly want to stick with 4.1. I discussed it here: http://blog.oneunicorn.com/2011/03/31/configuring-database-initializers-in-a-config-file/

19
3/13/2012 5:10:38 AM

Popular Answer

If I'm understanding you right, yourSeedSampleData initializer is just used for debugging, right?

You may use preprocessor directives to regulate this even though I'm not sure if there is a configuration setting to do so:

#if DEBUG
System.Data.Entity.Database.SetInitializer(new Domain.Concrete.SeedSampleData());
#else
System.Data.Entity.Database.SetInitializer(new System.Data.Entity.CreateDatabaseIfNotExists<EfDbContext>();
#endif

(presuming, naturally, that you don't use debug assemblies in live applications...)



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow