How do I handle situations in which I need pre-existing data before the app is started or right after the database is generated. For example, I have a list of countries in which I’d like to load into the database after code-first generates it. How do I do this?
App is structured as follows:
Repository > Service > WebMVC
The xml is in the WebMVC project.
You create custom initializer, which inherits from
DropCreateDatabaseIfModelChangesorDropCreateDatabaseAlwaysinterface. Like:And then you overwrite Seed method like:
Whole example might look like:
Edit: After setting this up, you have to set up Initializer too, as Ladislav Mrnka mentioned.
ie.: in Global.asax:
Don’t forget to add
using System.Data.Entity;…..