IDocumentClient.CreateDatabaseIfNotExistsAsync(Database, RequestOptions) Method


Creates(if doesn't exist) or gets(if already exists) a database resource as an asychronous operation in the Azure Cosmos DB service. You can check the status code from the response to determine whether the database was newly created(201) or existing database was returned(200)

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Database>> CreateDatabaseIfNotExistsAsync (Microsoft.Azure.Documents.Database database, Microsoft.Azure.Documents.Client.RequestOptions options = default);
abstract member CreateDatabaseIfNotExistsAsync : Microsoft.Azure.Documents.Database * Microsoft.Azure.Documents.Client.RequestOptions -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Database>>
Public Function CreateDatabaseIfNotExistsAsync (database As Database, Optional options As RequestOptions = Nothing) As Task(Of ResourceResponse(Of Database))



The specification for the Database to create.


(Optional) The RequestOptions for the request.



The Database that was created within a task object representing the service response for the asynchronous operation.


If database is not set.

Represents a consolidation of failures that occured during async processing. Look within InnerExceptions to find the actual exception(s).

This exception can encapsulate many different types of errors. To determine the specific error always look at the StatusCode property.


The example below creates a new Database with an Id property of 'MyDatabase' This code snippet is intended to be used from within an asynchronous method as it uses the await keyword

using (IDocumentClient client = new DocumentClient(new Uri("service endpoint"), "auth key"))
    Database db = await client.CreateDatabaseIfNotExistsAsync(new Database { Id = "MyDatabase" });

If you would like to construct a Database from within a synchronous method then you need to use the following code

using (IDocumentClient client = new DocumentClient(new Uri("service endpoint"), "auth key"))
    Database db = client.CreateDatabaseIfNotExistsAsync(new Database { Id = "MyDatabase" }).Result;

Applies to

See also