public async Task <IReadOnlyCollection <ICommittedDomainEvent> > CommitEventsAsync( IIdentity id, IReadOnlyCollection <SerializedEvent> serializedEvents, CancellationToken cancellationToken) { if (!serializedEvents.Any()) { return(new ICommittedDomainEvent[] { }); } var eventDataModels = serializedEvents .Select((e, i) => new EventDataModel { AggregateId = id.Value, AggregateName = e.Metadata[MetadataKeys.AggregateName], BatchId = Guid.Parse(e.Metadata[MetadataKeys.BatchId]), Data = e.SerializedData, Metadata = e.SerializedMetadata, AggregateSequenceNumber = e.AggregateSequenceNumber, }) .ToList(); _log.Verbose( "Committing {0} events to MSSQL event store for entity with ID '{1}'", eventDataModels.Count, id); const string sql = @" INSERT INTO EventFlow (BatchId, AggregateId, AggregateName, Data, Metadata, AggregateSequenceNumber) VALUES (@BatchId, @AggregateId, @AggregateName, @Data, @Metadata, @AggregateSequenceNumber); SELECT last_insert_rowid() FROM EventFlow"; IReadOnlyCollection <long> ids; try { ids = await _connection.InsertMultipleAsync <long, EventDataModel>( Label.Named("sqlite-insert-events"), cancellationToken, sql, eventDataModels) .ConfigureAwait(false); } catch (SQLiteException e) { if (e.ResultCode == SQLiteErrorCode.Constraint) { throw new OptimisticConcurrencyException(e.ToString(), e); } throw; } eventDataModels = eventDataModels .Zip( ids, (e, i) => { e.GlobalSequenceNumber = i; return(e); }) .ToList(); return(eventDataModels); }