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?
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.
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With