public static async Task Initialize(IDocumentDbClientFactory documentDbClientFactory) { DatabaseId = ConfigurationManager.AppSettings.GetRefValue <string>(DatabaseIdConfigName); CollectionId = ConfigurationManager.AppSettings.GetRefValue <string>(CollectionNameConfigName); IDocumentClient client = documentDbClientFactory.GetClient(); try { await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(DatabaseId)); } catch (DocumentClientException ex) { if (ex.StatusCode == HttpStatusCode.NotFound) { await client.CreateDatabaseAsync(new Database { Id = DatabaseId }); } } try { await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId)); } catch (DocumentClientException ex) { if (ex.StatusCode == HttpStatusCode.NotFound) { var documentCollection = new DocumentCollection { Id = CollectionId, IndexingPolicy = CollectionIndexingPolicy, }; await client.CreateDocumentCollectionAsync(UriFactory.CreateDatabaseUri(DatabaseId), documentCollection, CollectionRequestOptions); } } }
protected DocumentDbRepository(IDocumentDbClientFactory documentDbInitializer, string partitionKey = null) { PartitionKey = partitionKey; Client = documentDbInitializer.GetClient(); }