简体   繁体   English

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

[英]Entity Framework core seed large data from file

In EF Core 2.1 I can seed data this way:在 EF Core 2.1 中,我可以通过以下方式播种数据:

modelBuilder.Entity<Company>().HasData(
  new Company {.....},
  new Company {.....});

But I need to seed a text file with a large amount of rows (about 70k).但是我需要为一个包含大量行(大约 70k)的文本文件做种子。 What do you recommend me to achieve this?你建议我做什么来实现这一目标?

What format is the data in this text file in?这个文本文件中的数据是什么格式的?

If it's in JSON, you could do something like:如果它在 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();

Ok, here's a pretty good explanation: Parsing Tab delimited text files好的,这里有一个很好的解释: 解析制表符分隔的文本文件

Just add your inserts into the database after each "record".只需在每个“记录”之后将您的插入添加到数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM