Пример #1
0
        public async Task UpsertAsync(MongoContentEntity content, long oldVersion)
        {
            try
            {
                content.DataText = content.DataDraftByIds.ToFullText();

                await Collection.ReplaceOneAsync(x => x.Id == content.Id && x.Version == oldVersion, content, Upsert);
            }
            catch (MongoWriteException ex)
            {
                if (ex.WriteError.Category == ServerErrorCategory.DuplicateKey)
                {
                    var existingVersion =
                        await Collection.Find(x => x.Id == content.Id).Only(x => x.Id, x => x.Version)
                        .FirstOrDefaultAsync();

                    if (existingVersion != null)
                    {
                        throw new InconsistentStateException(existingVersion["vs"].AsInt64, oldVersion, ex);
                    }
                }
                else
                {
                    throw;
                }
            }
        }
        public Task UpsertAsync(MongoContentEntity content)
        {
            content.DataText       = content.DataByIds.ToFullText();
            content.DataDraftByIds = null;
            content.ScheduleJob    = null;
            content.ScheduledAt    = null;

            return(Collection.ReplaceOneAsync(x => x.Id == content.Id, content, new UpdateOptions {
                IsUpsert = true
            }));
        }