c# - Entity Framework: Only Seed when building initial database -


i'm using entity framework 6 dbmigrationsconfiguration:

public sealed class configuration : dbmigrationsconfiguration<datacontext> {     public configuration()     {         automaticmigrationsenabled = true;     }      protected override void seed(danfoss.energyefficiency.data.datacontext context)     {         //adding initial data context          context.savechanges();     }  } 

i'm using in webapi in way:

public static void register(httpconfiguration config) {     database.setinitializer(new migratedatabasetolatestversion<datacontext, configuration>()); } 

i have noticed seed function running every time application start up. how can prevent this? run first time runs, when build initial tables.

the dbmigrationsconfiguration.seed method called every time call update-database. reasoning behind explained in blog one unicorn.

that means have write seed code cope existing data. if don't that, can vote change on codeplex.

in meantime, quote blog:

the best way handle not use addorupdate every entity, instead more intentional checking database existing data using mechanisms appropriate. example, seed might check whether or not 1 representative entity exists , branch on result either update or insert everything

another option, have used in past, add standing data related migration in migration itself, using sql command. way runs once. have tended move away because prefer keep seeding in 1 place.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -