How do I populate data in Entity Framework code first on database creation?

code-first ef-code-first entity-framework

Question

I'm using code first entity framework and I would like to know how to run some code when the database is being created so that I can populate my database with some data. (note that I'm asking for on database creation, not everytime the application starts).

Does anybody know if there's a method or event I can use for this?

1
7
1/4/2011 5:52:18 AM

Accepted Answer

You can use custom database initializer - it means implementing IDatabaseInitializer and registering this initializer by calling Database.SetInitializer.

7
1/4/2011 9:10:25 AM

Popular Answer

I'd like to add to Ladislav's answer. The article he pointed to shows how to create an initializer but does not show how to use an initializer to populate a newly created database. DbInitializer has a method called Seed that you can overwrite. You can see an example of how to use this in this article (or video if you prefer, which is on the same page) http://msdn.microsoft.com/en-us/data/hh272552



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow