Note:
This documentation is for version 4.15.0 of the library.
Some samples may not work with other versions.
Installation
Install the Google.Cloud.Datastore.V1 package from NuGet. Add it to
your project in the normal way (for example by right-clicking on the
project in Visual Studio and choosing "Manage NuGet Packages...").
Authentication
When running on Google Cloud, no action needs to be taken to authenticate.
Otherwise, the simplest way of authenticating your API calls is to
set up Application Default Credentials.
The credentials will automatically be used to authenticate. See
Set up Application Default Credentials for more details.
The DatastoreDb
class is provided as a wrapper for
DatastoreClient,
simplifying operations considerably by assuming all operations act
on the same partition, and providing page streaming operations on
structured query results.
Several custom conversions, additional constructors,
factory methods (particularly on Filter
are provided to simplify working with the protobuf messages.
Support for emulator detection
As of 2.2.0-beta02 and 2.2.0, the library has support for detecting the
emulator via environment variables, if requested. This is configured
via DatastoreDbBuilder, which can also be used to configure custom
credentials easily.
The following code creates a DatastoreDb which will use the
emulator when the environment variables are present, but will
otherwise connect to the production environment.
DatastoreDb db = new DatastoreDbBuilder
{
ProjectId = projectId,
EmulatorDetection = EmulatorDetection.EmulatorOrProduction
}.Build();
// Use db as normal
See the
EmulatorDetection
enum for details of the other possible values.
Sample code
Inserting data
DatastoreDb db = DatastoreDb.Create(projectId, namespaceId);
KeyFactory keyFactory = db.CreateKeyFactory("message");
Entity entity = new Entity
{
Key = keyFactory.CreateIncompleteKey(),
["created"] = DateTime.UtcNow,
["text"] = "Text of the message",
["tags"] = new[] { "tag1", "tag2" }
};
using (DatastoreTransaction transaction = db.BeginTransaction())
{
transaction.Insert(entity);
CommitResponse commitResponse = transaction.Commit();
Key insertedKey = commitResponse.MutationResults[0].Key;
Console.WriteLine($"Inserted key: {insertedKey}");
// The key is also propagated to the entity
Console.WriteLine($"Entity key: {entity.Key}");
}
Querying data
DatastoreDb db = DatastoreDb.Create(projectId, namespaceId);
// Print the messages created in the last 5 minutes, most recent first
DateTime cutoff = DateTime.UtcNow.AddMinutes(-5);
Query query = new Query("message")
{
Filter = Filter.GreaterThanOrEqual("created", cutoff),
Order = { { "created", Direction.Descending } }
};
foreach (Entity entity in db.RunQueryLazily(query))
{
DateTime created = (DateTime)entity["created"];
string text = (string)entity["text"];
Console.WriteLine($"{created:yyyy-MM-dd'T'HH:mm:ss}: {text}");
}
When a query contains a projection, any timestamp fields will be
returned using integer values. Use the
Value.ToDateTimeFromProjection and
Value.ToDateTimeOffsetFromProjection methods to convert
either integer or timestamp values to DateTime or DateTimeOffset.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eGoogle.Cloud.Datastore.V1\u003c/code\u003e library is a .NET client for interacting with the Google Cloud Datastore API.\u003c/p\u003e\n"],["\u003cp\u003eVersion \u003ccode\u003e4.15.0\u003c/code\u003e is the latest version of the library, and documentation is provided for this version and multiple other earlier versions.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication with the library on Google Cloud happens automatically, and otherwise Application Default Credentials can be set up for authentication.\u003c/p\u003e\n"],["\u003cp\u003eThe library offers support for emulator detection, allowing it to seamlessly switch between emulator and production environments based on the presence of specific environment variables.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eDatastoreDb\u003c/code\u003e class simplifies operations by acting as a wrapper for \u003ccode\u003eDatastoreClient\u003c/code\u003e and by providing page streaming operations.\u003c/p\u003e\n"]]],[],null,["Version latestkeyboard_arrow_down\n\n- [4.15.0 (latest)](/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest)\n- [4.14.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.14.0)\n- [4.13.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.13.0)\n- [4.12.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.12.0)\n- [4.11.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.11.0)\n- [4.10.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.10.0)\n- [4.9.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.9.0)\n- [4.8.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.8.0)\n- [4.7.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.7.0)\n- [4.6.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.6.0)\n- [4.5.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.5.0)\n- [4.4.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.4.0)\n- [4.3.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.3.0)\n- [4.2.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.2.0)\n- [4.1.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.1.0)\n- [4.0.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/4.0.0)\n- [3.5.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/3.5.0)\n- [3.4.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/3.4.0)\n- [3.3.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/3.3.0)\n- [3.2.0](/dotnet/docs/reference/Google.Cloud.Datastore.V1/3.2.0) \n\nGoogle.Cloud.Datastore.V1\n=========================\n\n`Google.Cloud.Datastore.V1` is a.NET client library for the [Google Cloud Datastore API](https://cloud.google.com/datastore/docs/concepts/overview).\n\nNote:\nThis documentation is for version `4.15.0` of the library.\nSome samples may not work with other versions.\n\nInstallation\n------------\n\nInstall the `Google.Cloud.Datastore.V1` package from NuGet. Add it to\nyour project in the normal way (for example by right-clicking on the\nproject in Visual Studio and choosing \"Manage NuGet Packages...\").\n\nAuthentication\n--------------\n\nWhen running on Google Cloud, no action needs to be taken to authenticate.\n\nOtherwise, the simplest way of authenticating your API calls is to\nset up Application Default Credentials.\nThe credentials will automatically be used to authenticate. See\n[Set up Application Default Credentials](https://cloud.google.com/docs/authentication/provide-credentials-adc) for more details.\n\nGetting started\n---------------\n\nSee the [Datastore Quickstart](https://cloud.google.com/datastore/docs/quickstart) for an introduction with runnable code samples.\n\nThe [DatastoreDb](/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest/Google.Cloud.Datastore.V1.DatastoreDb)\nclass is provided as a wrapper for\n[DatastoreClient](/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest/Google.Cloud.Datastore.V1.DatastoreClient),\nsimplifying operations considerably by assuming all operations act\non the same partition, and providing page streaming operations on\nstructured query results.\n\nSeveral custom conversions, additional constructors,\nfactory methods (particularly on [Filter](/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest/Google.Cloud.Datastore.V1.Filter)\nare provided to simplify working with the protobuf messages.\n\nSupport for emulator detection\n------------------------------\n\nAs of 2.2.0-beta02 and 2.2.0, the library has support for detecting the\nemulator via environment variables, if requested. This is configured\nvia `DatastoreDbBuilder`, which can also be used to configure custom\ncredentials easily.\n\nThe following code creates a `DatastoreDb` which will use the\nemulator when the environment variables are present, but will\notherwise connect to the production environment. \n\n DatastoreDb db = new DatastoreDbBuilder\n {\n ProjectId = projectId,\n EmulatorDetection = EmulatorDetection.EmulatorOrProduction\n }.Build();\n // Use db as normal\n\nSee the\n[EmulatorDetection](/dotnet/docs/reference/Google.Cloud.Datastore.V1/latest/Google.Cloud.Datastore.V1.EmulatorDetection.yml)\nenum for details of the other possible values.\n\nSample code\n-----------\n\nInserting data\n--------------\n\n DatastoreDb db = DatastoreDb.Create(projectId, namespaceId);\n\n KeyFactory keyFactory = db.CreateKeyFactory(\"message\");\n Entity entity = new Entity\n {\n Key = keyFactory.CreateIncompleteKey(),\n [\"created\"] = DateTime.UtcNow,\n [\"text\"] = \"Text of the message\",\n [\"tags\"] = new[] { \"tag1\", \"tag2\" }\n };\n using (DatastoreTransaction transaction = db.BeginTransaction())\n {\n transaction.Insert(entity);\n CommitResponse commitResponse = transaction.Commit();\n Key insertedKey = commitResponse.MutationResults[0].Key;\n Console.WriteLine($\"Inserted key: {insertedKey}\");\n // The key is also propagated to the entity\n Console.WriteLine($\"Entity key: {entity.Key}\");\n }\n\nQuerying data\n-------------\n\n DatastoreDb db = DatastoreDb.Create(projectId, namespaceId);\n\n // Print the messages created in the last 5 minutes, most recent first\n DateTime cutoff = DateTime.UtcNow.AddMinutes(-5);\n Query query = new Query(\"message\")\n {\n Filter = Filter.GreaterThanOrEqual(\"created\", cutoff),\n Order = { { \"created\", Direction.Descending } }\n };\n foreach (Entity entity in db.RunQueryLazily(query))\n {\n DateTime created = (DateTime)entity[\"created\"];\n string text = (string)entity[\"text\"];\n Console.WriteLine($\"{created:yyyy-MM-dd'T'HH:mm:ss}: {text}\");\n }\n\nWhen a query contains a projection, any timestamp fields will be\nreturned using integer values. Use the\n`Value.ToDateTimeFromProjection` and\n`Value.ToDateTimeOffsetFromProjection` methods to convert\neither integer or timestamp values to `DateTime` or `DateTimeOffset`.\n\nLots more samples:\n[github.com/googleapis/dotnet-docs-samples](https://github.com/GoogleCloudPlatform/dotnet-docs-samples/tree/master/datastore/api)"]]