I am using an ASP.NET Core 1.0 (previously known as ASP.NET 5) application with the core framework and Entity Framework Core 1.0 (previously known as Entity Framework 7).
In this blog https://blogs.msdn.microsoft.com/dotnet/2015/11/18/entity-framework-7-rc1-available the people in charge of development of EF Core 1.0 say that it is not recommended to use an ORM like entity for bulk inserting a large amount of data and they recommend a lower level approach such as SqlBulkCopy to do so using the database provider directly and skipping Entity Framework.
I have 120,000 records that I need to seed. At the moment I am using something like this:
private readonly MyDbContext _context;
public MyDbContextSeedData(MyDbContext context)
{
_context = context;
}
public void EnsureSeedData(string seedPortsFilePath)
{
SeedPorts(seedPortsFile);
}
private void SeedPorts(string seedPortsFilePath)
{
if (!_context.Ports.Any())
{
var ports = PortsData.GetPorts(seedPortsFile);
List<Port> listPorts = ports.ToList();
// the following statement commented out did not make any improvement
// _context.ChangeTracker.QueryTrackingBehavior = Microsoft.Data.Entity.QueryTrackingBehavior.NoTracking
_context.AddRange(portsRange);
_context.SaveChanges();
}
}
and I am calling the EnsureSeedData(path)
from my Startup.cs where I have an instance of MyDbContextSeedData
My findings and questions with this approach:
System.Data.SqlClient.SqlBulkCopy
because this class does not exist in the core framework. Any alternative?Any example on how to seed large amounts of data using ASP.NET Core framework dnxcore50 would be appreciated!
I believe you are mistaken about System.Data.SqlClient.SqlBulkCopy:
Do as they recommend, ditch entity framework (of whatever stripe) and use sql bulk copy if this is going to be an on going task. On the other hand, you say "seed" -- if this is a one time thing, just do it straight in the database, and then restore from backup for future "start from scratch" situations.