实体框架代码中的数据库初始化代码首先 [英] Database Initialization in Entity Framework Code First

查看:140
本文介绍了实体框架代码中的数据库初始化代码首先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学习EF Code First。我已阅读代码优先的数据库初始化策略了解实体框架代码中的数据库初始化器代码第一条。

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.

我正在建立一个足球应用程序,所以在初始化应用程序我想插入每个团队和玩家名称到数据库(每个欧洲从最好的联赛 - 所以相当多的数据)。他们不会改变。它们是否应该在源代码中硬编码?或附加在xml中?简单文件?

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?

推荐答案

这取决于你在开发过程中的位置。您可以使用 Seed()方法 a>在创建数据库时运行的初始化器上。因此,如果你有大量的数据,并将频繁更改您的模型与DropCreate_____类型的初始化 - 我建议只是种子少量的数据进行测试。

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.

当您对初始设计感到满意时,可以执行一个完整的Seed()并切换到数据库迁移以处理模型更改。这将保留您现有的数据,并有新的数据的自己的Seed()方法。

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.

至于获取数据的地方,你可以看看这样的地方可以从网络服务获取或下载到csv,xml等。 http://www.jokecamp.com/blog/guide-to-football-and-soccer-data-and-apis/#footballdata 或此处 http://openfootball.github.io/

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/

这篇关于实体框架代码中的数据库初始化代码首先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆