I have an MVC app/api that accepts a JSON request from an AngularJs application. The request contains: a Lead property, a VisitorID (Guid), and a collection of schools. For the collection of schools I generate tasks that will submit each school to it's appropriate destination and return a SubmissionEvent object. I call Task.WhenAll() on the collection of tasks to run them in parallel, and when it finishes I flatten the collection of SubmissionEvents into an array. Then I BulkInsert them into my database. Easy peasy. Everything works.
The way the front-end angular app works, no more than 3 schools should be submitted at a time, and therefore BulkInsert should not be inserting more than 3 schools at a time.
Randomly, and only in production environment, BulkInsert will sometimes insert literally thousands (20k at one point) of records, all with the same info. The primary key auto-increments on insert, as well as the timestamp.
using (var scope = new TransactionScope())
{
// BulkInsert submissionEvents.
var options = new BulkInsertOptions { EnableStreaming = true, };
if (submissionEvents != null && submissionEvents.Count() > 0)
db.BulkInsert(submissionEvents, options);
scope.Complete();
}
How is this happening and how do I fix it?