c# - How to pre/populate database records with CodeFirst approach? -
i'm trying write application using tdd , codefirst in asp.net mvc following 20486b training course guide, i've stumbled upon technical/methodology issue i'll try describe here. when i'll coding application i'd prepopulate database rebuilt each time build/run/publish app (as understanding codefirst). first came mind create initial_data.sql script file bunch of inserts/updates on tables fired every build/run/publish of app. may work small initial data set, when have modify application, have larger set of data in database?
how can "include" initial_data.sql file each time database being rebuilt? , second question
how can codefirst on existing database preserving records in there?
override seed() method in configuration class , load data in there.
the seed() method run every time database updated / migrations run make sure handles existing data gracefully without duplicate inserts etc.
in seed() method have access dbcontext() instance can use execute sql, means can use ordinary ef api update date. addorupdate() extension great use loading initial data.
Comments
Post a Comment