I have what seems like a common problem, but I canโt figure out how to achieve the desired result. I have a nested object with navigation properties defined on it, as shown in the following diagram.

The collection of map points can potentially be quite large for a given MapLine, and for MapLayer there can be quite a large number of MapLines.
The question is, what is the best way to use the MapLayer object inserted into the database using the Entity Framework and maintain the relationships that are determined by the navigation properties?
Standard implementation of Entity Framework
dbContext.MapLayers.Add(mapLayer); dbContext.SaveChanges();
causes a big burst of memory and pretty bad return times.
I tried to implement the EntityFramework.BulkInsert package but it does not respect object relationships.
It seems that this would be a problem that someone has encountered before, but I cannot find any resources explaining how to complete this task.
Update
I tried to implement Richard's suggestion, but I donโt understand how to do this for a nested object such as the one I described. I assume that I need to insert a MapLayer object, then MapLines, then MapPoints to honor the PF / FK relationships in the database. I am currently trying to execute the following code, but this does not seem to be correct.
dbContext.MapLayers.Add(mapLayer); dbContext.SaveChanges(); List<MapLine> mapLines = new List<MapLine>(); List<MapPoint> mapPoints = new List<MapPoint>(); foreach (MapLine mapLine in mapLayer.MapLines) { //Update the mapPoints.MapLine properties to reflect the current line object var updatedLines = mapLine.MapPoints.Select(x => { x.MapLine = mapLine; return x; }).ToList(); mapLines.AddRange(updatedLines); } using (TransactionScope scope = new TransactionScope()) { MyDbContext context = null; try { context = new MyDbContext(); context.Configuration.AutoDetectChangesEnabled = false; int count = 0; foreach (var entityToInsert in mapLines) { ++count; context = AddToContext(context, entityToInsert, count, 100, true); } context.SaveChanges(); } finally { if (context != null) context.Dispose(); } scope.Complete(); }
Update 2
After several attempts to achieve this, I finally gave up and simply inserted MapLayer as an object and saved the MapLines => MapPoints relation as the Json source string in the byte array in the MapLayer object (since I am not asking for those structures, this works for me).
As the saying goes: "It's not bad, but it works."
I had some success with BulkInsert and relationship management outside of EF, but again there was a memory problem when trying to use EF to return data to the system. It seems that at present EF is not able to efficiently process large datasets and complex relationships.