c# - Update database from CodeFirst in EF6 without wiping out data -


i want use code first technique of ef6 when made changes table, drops database , recreates it, wiping out data. there way stop happening?

my code :

database.setinitializer<employeedb>(new dropcreatedatabaseifmodelchanges<employeedb>()); 

these strategies database initilization in code first approach:

  1. createdatabaseifnotexists: default initializer. name suggests, create database if none exists per configuration. however, if change model class , run application initializer, throw exception.
  2. dropcreatedatabaseifmodelchanges: initializer drops existing database , creates new database, if model classes (entity classes) have been changed. don't have worry maintaining database schema, when model classes change.
  3. dropcreatedatabasealways: name suggests, initializer drops existing database every time run application, irrespective of whether model classes have changed or not. useful, when want fresh database, every time run application, while developing application.
  4. custom db initializer: can create own custom initializer, if of above doesn't satisfy requirements or want other process initializes database using above initializer.

here, can give general idea , how use 1 of these approaches.

due comments createdatabaseifnotexists helps you. approach when add or remove model classes, db updated , data stable.

here can find examples both context constructor , config file

another topic this on stackoverflow.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -