コード例 #1
0
        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);
                }
            }
        }
コード例 #2
0
 protected DocumentDbRepository(IDocumentDbClientFactory documentDbInitializer, string partitionKey = null)
 {
     PartitionKey = partitionKey;
     Client       = documentDbInitializer.GetClient();
 }