4.6.0 (latest) 4.5.0 4.4.0 4.3.0 4.2.0 4.1.0 4.0.0 3.13.0 3.12.0 3.11.0 3.10.0 3.9.0 3.8.0 3.7.0 3.6.0 3.5.1 3.4.0 3.3.0 3.2.0 3.1.0 3.0.0 2.5.0 2.4.0 2.3.1

    public sealed class Transaction

Reference documentation and code samples for the Firestore API class Transaction.

A transaction, as created by
[RunTransactionAsync\<T\>(Func\<Transaction, Task\<T\>\>, TransactionOptions, CancellationToken)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.FirestoreDb#Google_Cloud_Firestore_FirestoreDb_RunTransactionAsync__1_System_Func_Google_Cloud_Firestore_Transaction_System_Threading_Tasks_Task___0___Google_Cloud_Firestore_TransactionOptions_System_Threading_CancellationToken_)
(and overloads) and passed to user code.

## Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) \> Transaction

## Inherited Members

[object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode) [object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype) [object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)

## Namespace

[Google.Cloud.Firestore](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore)

## Assembly

Google.Cloud.Firestore.dll

## Properties

### CancellationToken

    public CancellationToken CancellationToken { get; }

The cancellation token for this transaction

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken` |   |

### Database

    public FirestoreDb Database { get; }

The database for this transaction.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.FirestoreDb` |   |

## Methods

### Create(DocumentReference, object)

    public void Create(DocumentReference documentReference, object documentData)

Adds an operation to create a document in this transaction.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `documentReference` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.DocumentReference` The document reference to create. Must not be null. |
| `documentData` | `https://learn.microsoft.com/dotnet/api/system.object` The data for the document. Must not be null. |

### Delete(DocumentReference, Precondition)

    public void Delete(DocumentReference documentReference, Precondition precondition = null)

Adds an operation to delete a document's data in this transaction.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `documentReference` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.DocumentReference` The document to delete. Must not be null. |
| `precondition` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.Precondition` Optional precondition for deletion. May be null, in which case the deletion is unconditional. |

### GetAllSnapshotsAsync(IEnumerable\<DocumentReference\>, FieldMask, CancellationToken)

    public Task<IList<DocumentSnapshot>> GetAllSnapshotsAsync(IEnumerable<DocumentReference> documentReferences, FieldMask fieldMask, CancellationToken cancellationToken = default)

Fetch snapshots of all the documents specified by `documentReferences`, with respect to this transaction,
potentially limiting the fields returned.
This method cannot be called after any write operations have been created.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `documentReferences` | `https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable-1https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.DocumentReference` The document references to fetch. Must not be null, or contain null references. |
| `fieldMask` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.FieldMask` The field mask to use to restrict which fields are retrieved. May be null, in which case no field mask is applied, and the complete documents are retrieved. |
| `cancellationToken` | `https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken` A cancellation token to monitor for the asynchronous operation. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| ``https://learn.microsoft.com/dotnet/api/system.threading.tasks.task-1System.Collections.Generic.IList`1https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.DocumentSnapshot`` | The document snapshots, in the same order as `documentReferences`. |

**Remarks** Any documents which are missing are represented in the returned list by a [DocumentSnapshot](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.DocumentSnapshot)
with [Exists](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.DocumentSnapshot#Google_Cloud_Firestore_DocumentSnapshot_Exists) value of `false`.

### GetAllSnapshotsAsync(IEnumerable\<DocumentReference\>, CancellationToken)

    public Task<IList<DocumentSnapshot>> GetAllSnapshotsAsync(IEnumerable<DocumentReference> documentReferences, CancellationToken cancellationToken = default)

Fetch snapshots of all the documents specified by `documentReferences`, with respect to this transaction.
This method cannot be called after any write operations have been created.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `documentReferences` | `https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable-1https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.DocumentReference` The document references to fetch. Must not be null, or contain null references. |
| `cancellationToken` | `https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken` A cancellation token to monitor for the asynchronous operation. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| ``https://learn.microsoft.com/dotnet/api/system.threading.tasks.task-1System.Collections.Generic.IList`1https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.DocumentSnapshot`` | The document snapshots, in the same order as `documentReferences`. |

**Remarks** Any documents which are missing are represented in the returned list by a [DocumentSnapshot](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.DocumentSnapshot)
with [Exists](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.DocumentSnapshot#Google_Cloud_Firestore_DocumentSnapshot_Exists) value of `false`.

### GetSnapshotAsync(AggregateQuery, CancellationToken)

    public Task<AggregateQuerySnapshot> GetSnapshotAsync(AggregateQuery query, CancellationToken cancellationToken = default)

Fetch the [AggregateQuerySnapshot](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.AggregateQuerySnapshot) from the specified [AggregateQuery](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.AggregateQuery).

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `query` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.AggregateQuery` The aggregate query to execute. Must not be null. |
| `cancellationToken` | `https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken` A cancellation token to monitor for the asynchronous operation. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task-1https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.AggregateQuerySnapshot` | A [AggregateQuerySnapshot](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.AggregateQuerySnapshot) of results of the given query with respect to this transaction. |

### GetSnapshotAsync(DocumentReference, CancellationToken)

    public Task<DocumentSnapshot> GetSnapshotAsync(DocumentReference documentReference, CancellationToken cancellationToken = default)

Fetch a snapshot of the document specified by `documentReference`, with respect to this transaction.
This method cannot be called after any write operations have been created.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `documentReference` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.DocumentReference` The document reference to fetch. Must not be null. |
| `cancellationToken` | `https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken` A cancellation token to monitor for the asynchronous operation. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task-1https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.DocumentSnapshot` | A snapshot of the given document with respect to this transaction. |

### GetSnapshotAsync(Query, CancellationToken)

    public Task<QuerySnapshot> GetSnapshotAsync(Query query, CancellationToken cancellationToken = default)

Performs a query and returned a snapshot of the the results, with respect to this transaction.
This method cannot be called after any write operations have been created.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `query` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.Query` The query to execute. Must not be null. |
| `cancellationToken` | `https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken` A cancellation token to monitor for the asynchronous operation. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task-1https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.QuerySnapshot` | A snapshot of results of the given query with respect to this transaction. |

### Set(DocumentReference, object, SetOptions)

    public void Set(DocumentReference documentReference, object documentData, SetOptions options = null)

Adds an operation to set a document's data in this transaction.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `documentReference` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.DocumentReference` The document in which to set the data. Must not be null. |
| `documentData` | `https://learn.microsoft.com/dotnet/api/system.object` The data for the document. Must not be null. |
| `options` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.SetOptions` The options to use when updating the document. May be null, which is equivalent to [Overwrite](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.SetOptions#Google_Cloud_Firestore_SetOptions_Overwrite). |

### Update(DocumentReference, IDictionary\<FieldPath, object\>, Precondition)

    public void Update(DocumentReference documentReference, IDictionary<FieldPath, object> updates, Precondition precondition = null)

Adds an operation to update a document's data in this transaction.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `documentReference` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.DocumentReference` The document to update. Must not be null. |
| `updates` | `https://learn.microsoft.com/dotnet/api/system.collections.generic.idictionary-2https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.FieldPathhttps://learn.microsoft.com/dotnet/api/system.object` The updates to perform on the document, keyed by the field path to update. Fields not present in this dictionary are not updated. Must not be null or empty. |
| `precondition` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.Precondition` Optional precondition for updating the document. May be null, which is equivalent to [MustExist](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.Precondition#Google_Cloud_Firestore_Precondition_MustExist). |

### Update(DocumentReference, IDictionary\<string, object\>, Precondition)

    public void Update(DocumentReference documentReference, IDictionary<string, object> updates, Precondition precondition = null)

Adds an operation to update a document's data in this transaction.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `documentReference` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.DocumentReference` A document reference indicating the path of the document to update. Must not be null. |
| `updates` | `https://learn.microsoft.com/dotnet/api/system.collections.generic.idictionary-2https://learn.microsoft.com/dotnet/api/system.stringhttps://learn.microsoft.com/dotnet/api/system.object` The updates to perform on the document, keyed by the dot-separated field path to update. Fields not present in this dictionary are not updated. Must not be null or empty. |
| `precondition` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.Precondition` Optional precondition for updating the document. May be null, which is equivalent to [MustExist](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.Precondition#Google_Cloud_Firestore_Precondition_MustExist). |

### Update(DocumentReference, string, object, Precondition)

    public void Update(DocumentReference documentReference, string field, object value, Precondition precondition = null)

Adds an operation to update a document's data in this transaction.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `documentReference` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.DocumentReference` A document reference indicating the path of the document to update. Must not be null. |
| `field` | `https://learn.microsoft.com/dotnet/api/system.string` The dot-separated name of the field to update. Must not be null. |
| `value` | `https://learn.microsoft.com/dotnet/api/system.object` The new value for the field. May be null. |
| `precondition` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.Precondition` Optional precondition for updating the document. May be null, which is equivalent to [MustExist](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.Precondition#Google_Cloud_Firestore_Precondition_MustExist). |