来自数据源的 Codefirst 初始化器种子
而代码First Database Evolution 尚未推出,即使在开发过程中,我仍然希望在数据库中保留一些基本数据。我的想法是将数据保存在备份数据库中,并在初始化时自动将其读取到数据库中。
可以做这样的事情吗?
class MyInitializer : DropCreateDatabaseIfModelChanges<MyDatabase>
{
protected override void Seed(MyDatabase context)
{
var questions = new List<Question>
{
foreach(var row in MySecondDataSource)
{
new Question
{
QuestionID = row.Column["a"]
QuestionText = row.Column["b"],
SortOrder = row.Column["c"],
QuestionType = questionTypes.Find(q => q.QuestionTypeID == row.Column["d"]),
Category = categories.Find(c => c.CategoryID == row.Column["e"]),
Target = targets.Find(t => t.TargetID == row.Column["f"]),
Unit = units.Find(u => u.UnitID == row.Column["g"])
};
}
};
questions.ForEach(q => context.Questions.Add(q));
或者关于如何在删除数据后将数据保留或重新写入数据库的任何其他想法?
While the Code First Database Evolution is not yet available, I'd still like to keep some basic data in my database even during the development. My idea is to keep the data in a backup db, and automatically read it to the database on initialization.
Is it possible to do something like this?
class MyInitializer : DropCreateDatabaseIfModelChanges<MyDatabase>
{
protected override void Seed(MyDatabase context)
{
var questions = new List<Question>
{
foreach(var row in MySecondDataSource)
{
new Question
{
QuestionID = row.Column["a"]
QuestionText = row.Column["b"],
SortOrder = row.Column["c"],
QuestionType = questionTypes.Find(q => q.QuestionTypeID == row.Column["d"]),
Category = categories.Find(c => c.CategoryID == row.Column["e"]),
Target = targets.Find(t => t.TargetID == row.Column["f"]),
Unit = units.Find(u => u.UnitID == row.Column["g"])
};
}
};
questions.ForEach(q => context.Questions.Add(q));
Or any other ideas on how to go about keeping or reputting data to the database after dropping it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论