5.2.0 (latest) 5.1.0 5.0.0 4.17.0 4.16.0 4.15.0 4.14.0 4.13.0 4.12.0 4.11.0 4.10.0 4.9.0 4.8.0 4.7.0 4.6.0 4.5.0 4.4.0 4.3.0 4.2.0 4.1.0 4.0.0 3.5.0 3.4.0 3.3.0 3.2.0

    public sealed class KeyFactory

Reference documentation and code samples for the Google Cloud Datastore v1 API class KeyFactory.

Provides a convenient way of producing keys of a specific kind, from a specified parent entity or key,
or for root entities based on a partition ID.

## Inheritance

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

## 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.Datastore.V1](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest/Google.Cloud.Datastore.V1)

## Assembly

Google.Cloud.Datastore.V1.dll

## Remarks

If the key factory is constructed with just a partition ID and kind, then the parent key has an empty
path, and all keys created by the factory will be root keys. Otherwise, all keys created by the factory will
be direct children of the parent key, so both the parent key itself and any ancestors of that key will be
ancestors of the child key. Ancestors can be determined in queries using [HasAncestor(Key)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest/Google.Cloud.Datastore.V1.Filter#Google_Cloud_Datastore_V1_Filter_HasAncestor_Google_Cloud_Datastore_V1_Key_).

## Constructors

### KeyFactory(Entity, string)

    public KeyFactory(Entity parent, string kind)

Creates a key factory for children of the given key. Keys created by the new
factory will have the same path as this key, but with one extra path element.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `parent` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest/Google.Cloud.Datastore.V1.Entity` The "parent" entity for keys created by the new factory. Must not be null, and must have a key. |
| `kind` | `https://learn.microsoft.com/dotnet/api/system.string` The kind of child entity keys to create. Must not be null. |

**Remarks** The entity key is cloned before being stored, so may be mutated after the factory
is constructed without affecting the factory.

### KeyFactory(Key, string)

    public KeyFactory(Key parent, string kind)

Creates a key factory for children of the given key. Keys created by the new
factory will have the same path as this key, but with one extra path element.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `parent` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest/Google.Cloud.Datastore.V1.Key` The "parent" key for keys created by the new factory. Must not be null. |
| `kind` | `https://learn.microsoft.com/dotnet/api/system.string` The kind of child entity keys to create. Must not be null. |

**Remarks** The key is cloned before being stored, so may be mutated after the factory
is constructed without affecting the factory.

### KeyFactory(PartitionId, string)

    public KeyFactory(PartitionId partitionId, string kind)

Creates a key factory for the root of a partition.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `partitionId` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest/Google.Cloud.Datastore.V1.PartitionId` The partition ID for the factory. Must not be null. |
| `kind` | `https://learn.microsoft.com/dotnet/api/system.string` The kind of root entity keys to create. Must not be null. |

### KeyFactory(string, string, string)

    public KeyFactory(string projectId, string namespaceId, string kind)

Creates a key factory for the root of a partition.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `projectId` | `https://learn.microsoft.com/dotnet/api/system.string` The project ID for the factory. Must not be null. |
| `namespaceId` | `https://learn.microsoft.com/dotnet/api/system.string` The namespace ID for the factory. May be null in which case an empty string is used. |
| `kind` | `https://learn.microsoft.com/dotnet/api/system.string` The kind of root entity keys to create. Must not be null. |

### KeyFactory(string, string, string, string)

    public KeyFactory(string projectId, string namespaceId, string databaseId, string kind)

Creates a key factory for the root of a partition.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `projectId` | `https://learn.microsoft.com/dotnet/api/system.string` The project ID for the factory. Must not be null. |
| `namespaceId` | `https://learn.microsoft.com/dotnet/api/system.string` The namespace ID for the factory. May be null in which case an empty string is used. |
| `databaseId` | `https://learn.microsoft.com/dotnet/api/system.string` The database ID for the factory. May be null in which case an empty string is used. |
| `kind` | `https://learn.microsoft.com/dotnet/api/system.string` The kind of root entity keys to create. Must not be null. |

## Methods

### CreateIncompleteKey()

    public Key CreateIncompleteKey()

Creates a key with only the kind specified in the final path element, intended for insertions where
the ID is populated by the server.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest/Google.Cloud.Datastore.V1.Key` | A key with no name or ID in the final path element. |

### CreateKey(long)

    public Key CreateKey(long id)

Creates a key for the given entity ID.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `id` | `https://learn.microsoft.com/dotnet/api/system.int64` The numeric entity ID. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest/Google.Cloud.Datastore.V1.Key` | A key whose final path element contains the given ID. |

### CreateKey(string)

    public Key CreateKey(string name)

Creates a key for the given entity name.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `name` | `https://learn.microsoft.com/dotnet/api/system.string` The entity name. Must not be null. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest/Google.Cloud.Datastore.V1.Key` | A key whose final path element contains the given name. |