예제 #1
0
        private async Task <Microsoft.Azure.Documents.Database> GetOrCreateDatabaseAsync(Microsoft.Azure.Documents.Client.DocumentClient client, string databaseId)
        {
            var database = client.CreateDatabaseQuery().Where(db => db.Id == databaseId).ToArray().FirstOrDefault();

            if (database == null)
            {
                // Create the database.
                database = await client.CreateDatabaseAsync(new Microsoft.Azure.Documents.Database {
                    Id = databaseId
                });
            }

            return(database);
        }
예제 #2
0
        private static async Task CreateDatabaseIfNotExistsAsync()
        {
            #region Db

            if (_documentDatabase == null)
            {
                _documentDatabase = await _documentDbClient.CreateDatabaseAsync(new Db.Database
                {
                    Id = DatabaseStore
                });
            }

            #endregion

            #region Customer Collection

            if (_customerCollection == null)
            {
                // dbs/MyDatabaseId/colls/MyCollectionId/docs/MyDocumentId
                _customerCollection =
                    _documentDbClient.CreateDocumentCollectionQuery("dbs/" + _documentDatabase.Id)
                    .Where(c => c.Id == CustomerCollectionName)
                    .AsEnumerable()
                    .FirstOrDefault();

                // ReSharper disable once ConvertIfStatementToNullCoalescingExpression
                if (_customerCollection == null)
                {
                    _customerCollection =
                        await _documentDbClient.CreateDocumentCollectionAsync("dbs/" + _documentDatabase.Id,
                                                                              new Db.DocumentCollection
                    {
                        Id             = CustomerCollectionName,
                        IndexingPolicy =
                        {
                            Automatic    = true,
                            IndexingMode = Db.IndexingMode.Lazy
                        }
                    }, new Db.Client.RequestOptions
                    {
                        OfferType = "S1"
                    });
                }
            }

            #endregion
        }