Database Initialization in Entity Framework Code First

c# database ef-code-first entity-framework entity-framework-6

Question

I'm learning about EF Code First. I've read Database Initialization Strategies in Code-First and Understanding Database Initializers in Entity Framework Code First articles.

But I'm still confused, where should be the data before initialization? Those articles didn't mention about that and I think it's pretty important.

I'm building a football application, so while initializing app I'd like to insert every team and player name into database (every in the Europe from best leagues - so quite a lot of data). They will not change. Should they be hard-coded in sourcecode? or attached in xml? simple file?

Right now I have: before running, there is initialization prompt (Please wait, initialization...) I read the file, line by line inserting them into database. Is it good way?

1
2
4/27/2016 5:09:41 PM

Accepted Answer

It depends on where you are in the development process. You can use a Seed() method on the initializer which runs when the database is created. So if you have a ton of data and will be frequently changing your models with a DropCreate_____ type of initializer - I would recommend just seeding a small amount of data to test with.

When you are happy with your initial design, you can do a full Seed() and switch to database migrations to handle your model changes. This will keep your existing data and has it's own Seed() method for new data.

As to where to get the data, you can look at something like this where you could fetch from a web service or download into csv, xml, etc. http://www.jokecamp.com/blog/guide-to-football-and-soccer-data-and-apis/#footballdata or here http://openfootball.github.io/

2
4/27/2016 6:08:18 PM


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