Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework core seed large data from file

In EF Core 2.1 I can seed data this way:

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

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

like image 906
Yuri Morales Avatar asked Jun 01 '18 16:06

Yuri Morales


People also ask

Which method can be used to seed initial data in database using Entity Framework Core?

Seed Data in Entity Framework Core So as soon as we execute our migration files to create and configure the database, we want to populate it with some initial data. This action is called Data Seeding. So, we are using the HasData method to inform EF Core about the data it has to seed.

Should I use EF6 or EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

Is EF core faster than EF6?

Entity Framework (EF) Core, Microsoft's object-to-database mapper library for . NET Framework, brings performance improvements for data updates in version 7, Microsoft claims. The performance of SaveChanges method in EF7 is up to 74% faster than in EF6, in some scenarios.


1 Answers

What format is the data in this text file in?

If it's in JSON, you could do something like:

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();
like image 117
John-Luke Laue Avatar answered Sep 22 '22 21:09

John-Luke Laue