private async Task PrepareCollection(IMongoCollection <EventCommit> collection)
 {
     List <CreateIndexModel <EventCommit> > indexModels = IndexDefinitions.ProvideIndexModels().ToList();
     await indexModels.ForEachAsync(async model =>
     {
         await collection.Indexes.CreateOneAsync(model);
     });
 }
Exemple #2
0
        private Task PrepareCollection(IMongoCollection <EventCommit> collection)
        {
            var indexModels = IndexDefinitions.ProvideIndexModels().ToList();

            List <Task> tasks = new List <Task>(indexModels.Count);

            foreach (var model in indexModels)
            {
                tasks.Add(collection.Indexes.CreateOneAsync(model));
            }

            return(Task.WhenAll(tasks));
        }
        private async Task PrepareCollection(IMongoCollection <EventCommit> collection)
        {
            List <CreateIndexModel <EventCommit> > indexModels = IndexDefinitions.ProvideIndexModels().ToList();

            try
            {
                await collection.Indexes.CreateManyAsync(indexModels);
            }
            catch (MongoCommandException e) when(e.CodeName.Equals("IndexOptionsConflict",
                                                                   StringComparison.OrdinalIgnoreCase))
            {
                logger.LogWarning("Dropping existing index due to conflicts...");
                await collection.Indexes.DropAllAsync();

                logger.LogWarning("Recreating indexes");
                await collection.Indexes.CreateManyAsync(indexModels);
            }
        }