0

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:

  1. Insert at high volume + frequency without individual inserts
  2. 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

SB2055
  • 12,272
  • 32
  • 97
  • 202
  • 1
    Possible duplicate of [Fastest Way of Inserting in Entity Framework](https://stackoverflow.com/questions/5940225/fastest-way-of-inserting-in-entity-framework) – hatchet - done with SOverflow Aug 02 '17 at 20:55
  • @hatchet that answer is almost a decade old dude. I'd rather keep this open to get some fresh perspective on what's basically a different product. – SB2055 Aug 02 '17 at 21:17
  • Then try this [Entity Framework bulk insert unreal slow](https://stackoverflow.com/questions/36245732/entity-framework-bulk-insert-unreal-slow), or [Bulk insert using Entity Framework Extended](https://stackoverflow.com/questions/43164247/bulk-insert-using-entityframework-extended). These questions are all essentially the same as your question. – hatchet - done with SOverflow Aug 02 '17 at 21:24
  • There are several answer around. EF Extended is a good choice but If you need a free method check for my answers around (there is the source code). – bubi Aug 04 '17 at 06:50

0 Answers0