DocumentDB-データベースの作成
この章では、データベースの作成方法を学習します。Microsoft Azure DocumentDBを使用するには、DocumentDBアカウント、データベース、コレクション、およびドキュメントが必要です。すでにDocumentDBアカウントを持っていますが、データベースを作成するには2つのオプションがあります-
- MicrosoftAzureポータルまたは
- .Net SDK
MicrosoftAzureポータルを使用してDocumentDBのデータベースを作成する
ポータルを使用してデータベースを作成するには、次の手順に従います。
Step 1 − Azureポータルにログインすると、ダッシュボードが表示されます。

Step 2 −作成したDocumentDBアカウントをクリックすると、次のスクリーンショットに示すような詳細が表示されます。

Step 3 − [データベースの追加]オプションを選択し、データベースのIDを入力します。

Step 4 − [OK]をクリックします。

データベースが追加されていることがわかります。現時点ではコレクションはありませんが、JSONドキュメントを格納するコンテナーであるコレクションを後で追加できます。IDとリソースIDの両方があることに注意してください。
.NetSDKを使用してDocumentDBのデータベースを作成する
.Net SDKを使用してデータベースを作成するには、次の手順に従います。
Step 1 −前の章のVisualStudioでコンソールアプリケーションを開きます。
Step 2−新しいデータベースオブジェクトを作成して、新しいデータベースを作成します。新しいデータベースを作成するには、CreateDatabaseタスクで「mynewdb」に設定しているIdプロパティを割り当てるだけです。
private async static Task CreateDatabase(DocumentClient client) {
Console.WriteLine();
Console.WriteLine("******** Create Database *******");
var databaseDefinition = new Database { Id = "mynewdb" };
var result = await client.CreateDatabaseAsync(databaseDefinition);
var database = result.Resource;
Console.WriteLine(" Database Id: {0}; Rid: {1}", database.Id, database.ResourceId);
Console.WriteLine("******** Database Created *******");
}
Step 3−このdatabaseDefinitionをCreateDatabaseAsyncに渡し、Resourceプロパティを使用して結果を取得します。すべてのcreateobjectメソッドは、作成されたアイテム(この場合はデータベース)を説明するResourceプロパティを返します。
Resourceプロパティから新しいデータベースオブジェクトを取得し、DocumentDBが割り当てたリソースIDとともにコンソールに表示されます。
Step 4 − DocumentClientがインスタンス化された後、CreateDocumentClientタスクからCreateDatabaseタスクを呼び出します。
using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) {
await CreateDatabase(client);
}
以下は、これまでの完全なProgram.csファイルです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using Newtonsoft.Json;
namespace DocumentDBDemo {
class Program {
private const string EndpointUrl = "https://azuredocdbdemo.documents.azure.com:443/";
private const string AuthorizationKey = "BBhjI0gxdVPdDbS4diTjdloJq7Fp4L5RO/
StTt6UtEufDM78qM2CtBZWbyVwFPSJIm8AcfDu2O+AfV T+TYUnBQ==";
static void Main(string[] args) {
try {
CreateDocumentClient().Wait();
} catch (Exception e) {
Exception baseException = e.GetBaseException();
Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
}
Console.ReadKey();
}
private static async Task CreateDocumentClient() {
// Create a new instance of the DocumentClient
using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) {
await CreateDatabase(client);
}
}
private async static Task CreateDatabase(DocumentClient client) {
Console.WriteLine();
Console.WriteLine("******** Create Database *******");
var databaseDefinition = new Database { Id = "mynewdb" };
var result = await client.CreateDatabaseAsync(databaseDefinition);
var database = result.Resource;
Console.WriteLine(" Database Id: {0}; Rid: {1}", database.Id, database.ResourceId);
Console.WriteLine("******** Database Created *******");
}
}
}
上記のコードをコンパイルして実行すると、データベースIDとリソースIDを含む次の出力が表示されます。
******** Create Database *******
Database Id: mynewdb; Rid: ltpJAA==
******** Database Created *******