public async Task InsertWithHistoryAsync(EntityPackage newData, long user, Action <EntityPackage> modifyBeforeCreate = null)
        {
            if (newData.Entity.id > 0)
            {
                throw new InvalidOperationException("'New' package has non-zero id!");
            }

            var mainEntity = newData.Entity;
            await provider.WriteAsync(mainEntity);

            try
            {
                newData.Relink();
                modifyBeforeCreate?.Invoke(newData);

                var writes = new List <EntityBase>();

                //Must write everything else at the same time. We only wrote the first thing to get the ID
                writes.AddRange(newData.Values);
                writes.AddRange(newData.Relations);

                writes.Add(activityService.MakeActivity(newData.Entity, user, Keys.CreateAction));

                await provider.WriteAsync(writes.ToArray());
            }
            catch
            {
                logger.LogError("Failure during historic insert, trying to undo... Exception bubbling");
                await provider.DeleteAsync(mainEntity);

                throw;
            }
        }
        public EntityPackage ConvertHistoryToUpdate(EntityPackage history)
        {
            var result = new EntityPackage(history);

            //Pull out (literally) the history relation
            var historyLink = result.Relations.Where(x => x.type == Keys.HistoryRelation).OnlySingle();

            result.Relations.RemoveAll(x => x.type == Keys.HistoryRelation);

            //Update the id from history, relink to us (I don't know if relinking matters...)
            result.Entity.id = historyLink.entityId1;
            result.Relink();

            //Finally, mark the type as would normally be
            result.Entity.type = result.Entity.type.Substring(Keys.HistoryKey.Length);

            return(result);
        }