【发布时间】: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