来自数据源的 Codefirst 初始化器种子

发布于 2024-10-19 12:01:02 字数 1198 浏览 1 评论 0原文

代码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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文