How can I create (seed) a lot of static data in Entity Framework? By static data I mean data that is located in the database, but only exists once.
For instance in my database, I have a CountrySet, containing all the countries on the globe. I would like these countries to always be there, but the fact is that every time I wipe out my database and recreate it for new deployment, all that data is lost.
Creating it all when my application starts (if it’s not already created) appears to be quite time sensitive.
What can I do here?
First of all, let me ask this:
Are you familiar with the
Seedfunction which you gain access to when you set your ownInitializer?Example:
In your main function write something like this:
Where
SystemContextis yourDbContextClass.And in that class you should have something like:
That’s just some template code from a project I had lying around.
If you’re indeed re-creating your database every time then I don’t know of any other way to “seed” a database without “manually” adding in all the data.
On the other hand, if you plan on doing something other than dropping the database every time you want to change the details in it, you might have to manually write functions that clear out all the new data according to some logic.
I hope this answers your question 🙂
ALSO: You didn’t specify neither in the question nor using tags which technology you’re talking about. I assumed it to be c#, .net and entity-framework-4.3.1. If that is not the case, please re-tag / specify.
Have a nice day 🙂