【问题标题】:EF migration Seeding with large dataset大数据集的 EF 迁移播种
【发布时间】:2015-01-13 05:21:45
【问题描述】:

我在 ASP.NET MVC5 项目中使用 EF Code First Migration。我想用大约 5000 条记录为数据库播种。 asp.net网站和博客上的例子在配置方法中都有种子功能。

internal sealed class Configuration : DbMigrationsConfiguration<foo.ApplicationDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        MigrationsDirectory = @"Migrations\ApplicationDbContext";
    }

    protected override void Seed(foo.ApplicationDbContext context)
    {

        SeedProducts(context);
        SeedFoods(context);


        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }
}

在使用 EF 迁移时,有哪些选项可以为大型数据集播种?

目前配置文件超过5000行,管理方式不同。

我更愿意将它们存储在另一个数据库或 Excel 电子表格中,然后使用种子函数将它们导入。我不确定如何在 Seed 方法中从外部源导入数据。

我也尝试将数据集分解为几个文件,但是当我尝试调用该函数时

 SeedProducts(context);
 SeedFoods(context);

在配置类之外,我得到一个构建错误:“该名称在当前上下文中不存在”。 (我不确定这是什么意思?

【问题讨论】:

  • 为什么不首先为数据库播种(因为您已经有一个可用的种子函数),然后保存备份(或备份脚本),然后在(新)种子函数中运行该脚本?您可以像“手动”一样尽快从 Web API 或搜索引擎播种数据库。

标签: asp.net-mvc entity-framework-migrations


【解决方案1】:

您可以将sql文件存储在基本目录中并使用它。您可以创建多个 sql 文件。我使用以下代码来执行存储在基目录中的所有 sql 文件。

protected override void Seed(CRM.Data.DbContexts.CRMContext context)
        {


          var sqlfiles = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory+"\\initialdata", "*.sql");
          sqlfiles.ToList().ForEach(x=> context.Database.ExecuteSqlCommand(File.ReadAllText(x)));

        }

【讨论】:

    【解决方案2】:

    为什么我们需要 5000 条记录的种子数据。如果您更喜欢这种方式,也需要大量的手动工作。其中,这里不需要。

    您可以立即创建脚本并将其执行到您的数据库中。通过代码和手动。

    数据库脚本可以用于整个数据库以及每个表,存储过程也很明智。所以,你会得到特别的记录。

    按照this linkMSDN 中的步骤操作

    注意:创建数据库脚本之后。您可以从种子函数读取文件并从函数本身执行查询。 或者您可以在需要时手动执行。

    【讨论】:

      【解决方案3】:

      我最终使用了 CSV(逗号分隔文件)并将其存储为域资源。然后读取 CSV 文件并添加数据库记录:

      我可以使用 EF Migration Seed method and a CSV file 为数据库播种,如 Migration.cs 文件中的定义如下。注意:Visual Studio 中项目中的 CSV 文件设置为 Build Action to Embedded Resource。

      public void SeedData(WebApp.Models.ApplicationDbContext Context)
          {
              Assembly assembly = Assembly.GetExecutingAssembly();
              string resourceName = "WebApp.SeedData.Name.csv";
              using (Stream stream = assembly.GetManifestResourceStream(resourceName))
              {
                  using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                  {
                      CsvReader csvReader = new CsvReader(reader);
                      csvReader.Configuration.WillThrowOnMissingField = false;
                      var records = csvReader.GetRecords<Products>().ToArray();
      
                      foreach (Product record in records)
                      {                            
                            Context.Products.AddOrUpdate(record); 
                      }
                  }
              }
              Context.SaveChanges();
          }
      

      【讨论】:

        猜你喜欢
        • 2020-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-26
        • 2012-06-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多