Database.CreateContainerAsync Method

Definition

Overloads

CreateContainerAsync(ContainerProperties, Nullable<Int32>, RequestOptions, CancellationToken)

Creates a container as an asynchronous operation in the Azure Cosmos service.

CreateContainerAsync(String, String, Nullable<Int32>, RequestOptions, CancellationToken)

Creates a container as an asynchronous operation in the Azure Cosmos service.

CreateContainerAsync(ContainerProperties, Nullable<Int32>, RequestOptions, CancellationToken)

Creates a container as an asynchronous operation in the Azure Cosmos service.

public abstract System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ContainerResponse> CreateContainerAsync (Microsoft.Azure.Cosmos.ContainerProperties containerProperties, Nullable<int> throughput = null, Microsoft.Azure.Cosmos.RequestOptions requestOptions = null, System.Threading.CancellationToken cancellationToken = null);
abstract member CreateContainerAsync : Microsoft.Azure.Cosmos.ContainerProperties * Nullable<int> * Microsoft.Azure.Cosmos.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ContainerResponse>

Parameters

containerProperties
ContainerProperties

The ContainerProperties object.

throughput
Nullable<Int32>

(Optional) The throughput provisioned for a container in measurement of Requests Units per second in the Azure Cosmos DB service.

requestOptions
RequestOptions

(Optional) The options for the request.

cancellationToken
CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

A Task containing a ContainerResponse which wraps a ContainerProperties containing the read resource record.

Exceptions

If containerProperties is not set.

Represents a consolidation of failures that occurred 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. Some common codes you may get when creating a container are:

StatusCodeReason for exception
400BadRequest - This means something was wrong with the request supplied. It is likely that an id was not supplied for the new container.
403Forbidden - This means you attempted to exceed your quota for containers. Contact support to have this quota increased.
409Conflict - This means a ContainerProperties with an id matching the id you supplied already existed.

Examples


   ContainerProperties containerProperties = new ContainerProperties()
   {
       Id = Guid.NewGuid().ToString(),
       PartitionKeyPath = "/pk",
       IndexingPolicy = new IndexingPolicy()
      {
           Automatic = false,
           IndexingMode = IndexingMode.Lazy,
      };
   };
  
   ContainerResponse response = await this.cosmosDatabase.CreateContainerAsync(containerProperties);

Remarks

for details on provision throughput.

See also

CreateContainerAsync(String, String, Nullable<Int32>, RequestOptions, CancellationToken)

Creates a container as an asynchronous operation in the Azure Cosmos service.

public abstract System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ContainerResponse> CreateContainerAsync (string id, string partitionKeyPath, Nullable<int> throughput = null, Microsoft.Azure.Cosmos.RequestOptions requestOptions = null, System.Threading.CancellationToken cancellationToken = null);
abstract member CreateContainerAsync : string * string * Nullable<int> * Microsoft.Azure.Cosmos.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ContainerResponse>

Parameters

id
String

The Cosmos container id

partitionKeyPath
String

The path to the partition key. Example: /location

throughput
Nullable<Int32>

(Optional) The throughput provisioned for a container in measurement of Requests Units per second in the Azure Cosmos DB service.

requestOptions
RequestOptions

(Optional) The options for the request.

cancellationToken
CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

A Task containing a ContainerResponse which wraps a ContainerProperties containing the read resource record.

Exceptions

If id is not set.

Represents a consolidation of failures that occurred 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. Some common codes you may get when creating a container are:

StatusCodeReason for exception
400BadRequest - This means something was wrong with the request supplied. It is likely that an id was not supplied for the new container.
403Forbidden - This means you attempted to exceed your quota for containers. Contact support to have this quota increased.
409Conflict - This means a ContainerProperties with an id matching the id you supplied already existed.

Examples


   ContainerResponse response = await this.cosmosDatabase.CreateContainerAsync(Guid.NewGuid().ToString(), "/pk");

Remarks

for details on provision throughput.

See also

Applies to