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).
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.
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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["\u003cp\u003eThe latest version of the \u003ccode\u003eKeyFactory\u003c/code\u003e class within the Google Cloud Datastore V1 API is version 4.15.0.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eKeyFactory\u003c/code\u003e class provides a way to generate keys for a specific kind of entity, either as root entities or as children of a specified parent entity or key.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eKeyFactory\u003c/code\u003e allows for the creation of root keys based on a partition ID, or can be used to build child keys tied to an existing parent key or entity.\u003c/p\u003e\n"],["\u003cp\u003eConstructors for the \u003ccode\u003eKeyFactory\u003c/code\u003e include options to specify the parent entity or key, partition ID, project ID, namespace ID, database ID, and the kind of entity.\u003c/p\u003e\n"],["\u003cp\u003eMethods available within the \u003ccode\u003eKeyFactory\u003c/code\u003e class include the ability to \u003ccode\u003eCreateIncompleteKey()\u003c/code\u003e, \u003ccode\u003eCreateKey(long id)\u003c/code\u003e, and \u003ccode\u003eCreateKey(string name)\u003c/code\u003e, offering different options for key creation.\u003c/p\u003e\n"]]],[],null,["# Google Cloud Datastore v1 API - Class KeyFactory (4.15.0)\n\nVersion latestkeyboard_arrow_down\n\n- [4.15.0 (latest)](/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest/Google.Cloud.Datastore.V1.KeyFactory)\n- [4.14.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.14.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [4.13.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.13.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [4.12.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.12.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [4.11.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.11.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [4.10.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.10.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [4.9.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.9.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [4.8.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.8.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [4.7.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.7.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [4.6.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.6.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [4.5.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.5.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [4.4.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.4.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [4.3.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.3.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [4.2.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.2.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [4.1.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.1.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [4.0.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.0.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [3.5.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/3.5.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [3.4.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/3.4.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [3.3.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/3.3.0/Google.Cloud.Datastore.V1.KeyFactory)\n- [3.2.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/3.2.0/Google.Cloud.Datastore.V1.KeyFactory) \n\n public sealed class KeyFactory\n\nReference documentation and code samples for the Google Cloud Datastore v1 API class KeyFactory.\n\nProvides a convenient way of producing keys of a specific kind, from a specified parent entity or key,\nor for root entities based on a partition ID. \n\nInheritance\n-----------\n\n[object](https://learn.microsoft.com/dotnet/api/system.object) \\\u003e KeyFactory \n\nInherited Members\n-----------------\n\n[object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode) \n[object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype) \n[object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)\n\nNamespace\n---------\n\n[Google.Cloud.Datastore.V1](/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest/Google.Cloud.Datastore.V1)\n\nAssembly\n--------\n\nGoogle.Cloud.Datastore.V1.dll\n\nRemarks\n-------\n\nIf the key factory is constructed with just a partition ID and kind, then the parent key has an empty\npath, and all keys created by the factory will be root keys. Otherwise, all keys created by the factory will\nbe direct children of the parent key, so both the parent key itself and any ancestors of that key will be\nancestors of the child key. Ancestors can be determined in queries using [HasAncestor(Key)](/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest/Google.Cloud.Datastore.V1.Filter#Google_Cloud_Datastore_V1_Filter_HasAncestor_Google_Cloud_Datastore_V1_Key_).\n\nConstructors\n------------\n\n### KeyFactory(Entity, string)\n\n public KeyFactory(Entity parent, string kind)\n\nCreates a key factory for children of the given key. Keys created by the new\nfactory will have the same path as this key, but with one extra path element.\n\n**Remarks** \nThe entity key is cloned before being stored, so may be mutated after the factory\nis constructed without affecting the factory.\n\n### KeyFactory(Key, string)\n\n public KeyFactory(Key parent, string kind)\n\nCreates a key factory for children of the given key. Keys created by the new\nfactory will have the same path as this key, but with one extra path element.\n\n**Remarks** \nThe key is cloned before being stored, so may be mutated after the factory\nis constructed without affecting the factory.\n\n### KeyFactory(PartitionId, string)\n\n public KeyFactory(PartitionId partitionId, string kind)\n\nCreates a key factory for the root of a partition.\n\n### KeyFactory(string, string, string)\n\n public KeyFactory(string projectId, string namespaceId, string kind)\n\nCreates a key factory for the root of a partition.\n\n### KeyFactory(string, string, string, string)\n\n public KeyFactory(string projectId, string namespaceId, string databaseId, string kind)\n\nCreates a key factory for the root of a partition.\n\nMethods\n-------\n\n### CreateIncompleteKey()\n\n public Key CreateIncompleteKey()\n\nCreates a key with only the kind specified in the final path element, intended for insertions where\nthe ID is populated by the server.\n\n### CreateKey(long)\n\n public Key CreateKey(long id)\n\nCreates a key for the given entity ID.\n\n### CreateKey(string)\n\n public Key CreateKey(string name)\n\nCreates a key for the given entity name."]]