来自数据源的Codefirst Initializer Seed

时间:2011-03-01 14:22:03

标签: c# .net entity-framework-4 ef-code-first

虽然Code 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));

关于如何在放弃数据后保存或重新输入数据的任何其他想法?

0 个答案:

没有答案