I have a need to frequently push large numbers of rows into a table using EF. An example of my code:
foreach (InboundEventDto newEvent in newEvents) // array of ~300 events, every ~10 seconds
{
AppEvent dbEvent = new AppEvent
{
UserId = user.UserId,
EventType = newEvent.EventType,
// etc
};
eventsToSave.Add(dbEvent);
}
_db.Configuration.AutoDetectChangesEnabled = false;
_db.AppEvents.AddRange(eventsToSave);
_db.SaveChanges();
_db.Configuration.AutoDetectChangesEnabled = true;
}
However under the hood, AddRange
does 300 individual INSERTS
which is silly slow. I need to retrieve the added IDs, which referenced libraries don't seem to handle. Is there anything (free) out there that will allow me to:
- Insert at high volume + frequency without individual inserts
- Capture the added IDs after the bulk insert
Edit to explain how this is not a duplicate: again, looking for a free solution that returns inserted ids