asp.net mvc - Where to put Seed data using EF5 code-first migrations -


i'm starting out ef , mvc, , following different tutorials provide different confusing ideas on correct place put dummy test data in database.

so i've made poco classes, have blahcontext

public class blahcontext : dbcontext {     public dbset<blah> blahs { get; set; }      public blahcontext() : base("defaultconnection")     {         configuration.proxycreationenabled = false;     } } 

then in global.asax.cs initialize database this:

protected void application_start() {     database.setinitializer(new dropcreatedatabasealways<blahcontext>()); } 

now did following in package manager console:

pm>  enable-migrations -contexttypename blah.models.blahcontext pm>  add-migration initial pm>  update-database 

this created configuration.cs, , in class there seed() method - generated comments imply correct place put seed data. filled in this:

internal sealed class configuration : dbmigrationsconfiguration<blah.models.blahcontext> {     public configuration()     {         automaticmigrationsenabled = false;     }      protected override void seed(blah.models.blahcontext context)     { #if debug         context.blahs.addorupdate(b => b.id,             new blah() { id = 1, name = "bum", },             new blah() { id = 2, name = "moo", }         ); #endif     } } 

now when run update-database again, populates blah table 2 records "bum" , "moo". good.

but when run application, seems database.setinitializer() call in application_start causes database recreated, time configuration.seed() method not called. data no longer present.

i saw other tutorials created class such as:

class blahcontextinitializer : dropcreatedatabasealways<blahcontext> 

and application_start looks like:

protected void application_start() {     database.setinitializer(new blahcontextinitializer()); } 

and in blahcontextinitializer.seed() method that's dummy data contained. strategy ef4.1 only?

where should put seed data in ef5 doesn't wiped out when application starts?

but when run application, seems database.setinitializer() call in application_start causes database recreated, time configuration.seed() method not called. data no longer present.

you should use migratedatabasetolatestversion initializer if you're using migrations.
or skip initializer - if plan on manually migrating db (using update-database).

problem have two different initializers (or initialization scenarios precise) trying work together. have totally different 'plans'. it's problem other but dropcreatedatabasealways , has no way of working.

have in mind migration seed runs each start of application, it's bit different (than 'typical' seed).


if have complex scenarios (that require having typical 'seed' initializer (like done before) - see post of mine on how create custom initializer that...

what correct use of idatabaseinitializer in ef?

how create initializer create , migrate mysql database?


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -