来自文件的实体框架核心种子大数据

Yur*_*les 6 entity-framework asp.net-core

在 EF Core 2.1 中,我可以通过以下方式播种数据:

modelBuilder.Entity<Company>().HasData(
  new Company {.....},
  new Company {.....});
Run Code Online (Sandbox Code Playgroud)

But I need to seed a text file with a large amount of rows (about 70k). What do you recommend me to achieve this?

Nee*_*ack 5

这个文本文件中的数据是什么格式的?

如果它在 JSON 中,您可以执行以下操作:

var companies = new List<Company>();
using (StreamReader r = new StreamReader(@"C:\temp\data.json"))
{
    string json = r.ReadToEnd();
    companies = JsonConvert.DeserializeObject<List<Company>>(json);
}

foreach(var company in companies)
    dbContext.Companies.Add(company);

dbContext.SaveChanges();
Run Code Online (Sandbox Code Playgroud)