Megosztás a következőn keresztül:


CosmosContainer.CreateItemAsync<T> Method

Definition

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

public abstract System.Threading.Tasks.Task<Azure.Cosmos.ItemResponse<T>> CreateItemAsync<T> (T item, Azure.Cosmos.PartitionKey? partitionKey = default, Azure.Cosmos.ItemRequestOptions requestOptions = default, System.Threading.CancellationToken cancellationToken = default);
abstract member CreateItemAsync : 'T * Nullable<Azure.Cosmos.PartitionKey> * Azure.Cosmos.ItemRequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Cosmos.ItemResponse<'T>>
Public MustOverride Function CreateItemAsync(Of T) (item As T, Optional partitionKey As Nullable(Of PartitionKey) = Nothing, Optional requestOptions As ItemRequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of ItemResponse(Of T))

Type Parameters

T

Parameters

item
T

A JSON serializable object that must contain an id property. CosmosSerializer to implement a custom serializer

partitionKey
Nullable<PartitionKey>

Partition key for the item. If not specified will be populated by extracting from {T}

requestOptions
ItemRequestOptions

(Optional) The options for the item request ItemRequestOptions

cancellationToken
CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

The Response<T> that was created contained within a Task object representing the service response for the asynchronous operation.

Exceptions

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 Document are:

StatusCodeReason for exception
400BadRequest - This means something was wrong with the document supplied.
403Forbidden - This likely means the collection in to which you were trying to create the document is full.
409Conflict - This means a item with an id matching the id field of item already existed
413RequestEntityTooLarge - This means the item exceeds the current max entity size. Consult documentation for limits and quotas.
429TooManyRequests - This means you have exceeded the number of request units per second.

Examples

public class ToDoActivity{
    public string id {get; set;}
    public string status {get; set;}
}

ToDoActivity test = new ToDoActivity()
{
   id = Guid.NewGuid().ToString(),
   status = "InProgress"
};

ItemResponse item = await this.container.CreateItemAsync<ToDoActivity>(tests, new PartitionKey(test.status));

Applies to