A beginners guide to read/write data in to Google DataStore using .NET Core 2.1 Console Application.

Fiqri Ismail
Nov 9, 2018 · 7 min read

Before you begin, I assume you already have, .NET Core 2.1 installed in your PC or Mac. If not be my guest and install it in your machine. Here we go with the link

Follow the instructions in the download site and you are good to go. We will be going full command line and will use Visual Studio Code to write code. If you don’t have Visual Studio Code, visit the below link and you are good to go.

Umm, yes thats it, you have all the tools you need at your side. Next step is to have a Google Cloud Platform account. You can get a free account here.

Use your existing google account to create GCP account. Instructions in the page are fare simple to follow. All right !!! Let’s get our teeth in to this.

Preparing the Project

$ dotnet new console -o datastoreconsole 

This will create you the console application with .NET Core. Get into the newly created folder.

$ cd datastoreconsole 

And do a quick run using

$ dotnet run 
Hello World

Next step, we need to add the .NET client library for Google Datastore. You can simply execute this command in the shell.

$ dotnet add package Google.Cloud.Datastore.V1

And then do a restore to get the packages to your environment executing this command.

$ dotnet restore

Excellent !!! Let’s open the project folder using Visual Studio code and click on the project file (dotnetstoreconsole.csproj) and the <ItemGroup> should look like this.

OK, our local environment is almost done. Let’s prepare the GCP environment. For that we will need:

  1. A project at GCP
  2. A DataStore Database for the above project
  3. A Service Account with a key to access the DataStore

1. Create a project at GCP

After all those logins and stuff, you will reach the GCP Console. In the console, and in the left menu pane. click on IAM & admin and then Manage resources

Manage resources

This will lead you to the resources you have. Now in the top click on + CREATE PROJECT button. Give a project name and leave the default for Location. Click CREATE button. GCP will take few seconds to create the project.

New Project (dotnetDS)

Do you see that small line below the Project Name? Yep thats very important. Make sure you copy it down. You will need the Project ID to access the DatasStore.

2. Create the DataStore Database for the project

Now your top left menu bar should look like this,

Now, scroll down the left menu pane till you reach Datastore label. And click on the label. You will be presented with what sort of database service you need in your project. Click on the blue SELECT button, in the Cloud Datastore card.

Select Cloud Datastore

Now click the blue NEXT button bottom of the screen. Next stop will be for you to select a location close to you. Select one and hit CREATE DATABASE button.

Select a location

GCP will take few seconds to create your database. And one more task left in the list. Let’s create a Service Account to access this database.

3. Create a Service Account

In this page you need to give a Service account name, an id (Id will be automatically created for you, if you are not happy with the one create you can always choose one) and a description. Description is optional. And then click on the blue CREATE button.

STEP 1 — Create service account

Now you have to select a role for the service account. Click the “Select a role” drop down and select Owner

STEP 2 — Select a role

And then click the blue, CONTINUE button. Now one last step here. We need to create a key. Now click on the + CREATE KEY button. Select JSON in the popup and click the blue CREATE button.

STEP 3 — Select JSON

This will download a JSON file with information required to access services in the GCP. Make sure you copy it to a location and you will need it to access the Datastore.

Downloaded JSON

OK, we are done with the setup let’s do some coding.

Open Visual Studio Code and open the folder contains the console application we just created above. Let’s click on the “Program.cs” add the following using directive.

Lets add the following method right below the Main() method inside the “Program.cs”

CreateMovies() method

OK lets go through this code. In the line 3 we have created a string to store the projectId from GCP. As you remember we copied it down when we created the project. Line 6 we have tell what kind of an entity is this to the Datastore. In the line 8 we have created a Database object for the DataStore using the projectId we stored. Then we created an entity list. If you see at the line 17 we have created a key for our entity. This is a unique key identifier for each entity. And then lines 18 to 20 we have created key value pairs.

Finally from lines 25 to 29 we used transaction object to send the list of movies to the datastore.

Now replace the code in the Main() method with this one.

Now go back to your shell (or Command in Windows), execute the following command to setup the environment with the JSON key we downloaded. This is required to access the Datastore service.

The syntax should look like this

$  export GOOGLE_APPLICATION_CREDENTIALS=[path_to_your_key_file]

and the here is the example with my JSON key.

$  export GOOGLE_APPLICATION_CREDENTIALS=~/PlayGround/GCP/keys/dotnetds-222013-fe4248ec93fb.json

You are almost done. Next lets execute our console application and see. If everything is OK you should see an output like this.


Excellent. Lets make sure, all the data is sent to the Datastore. Now go back to your GCP Console. And click on the Google Cloud Platform logo on the top right of the window. In the menu scroll down and click Datastore


Your data should look like this in the Datastore

All your data at Datastore

Excellent. We have saved our data at the data store. Let’s retrieve them writing this method.

Go back to your “Program.cs” add the following GetMovies() method next to the CreateMovies() method.

GetMovies() method

Again in the above method we have created a query in line 9 and then executed the query in line 10, picked up all the movies. Finally created a string in line 12 with returned movies list.

And modify the Main() as shown below.

Now go back to your shell (or Command in Windows) execute the following command.

Listed Movies

Excellent and thats it. Enjoy coding.


Google Datastore

.NET Core

Grab the source code for this tutorial

Fiqri Ismail

Written by

Architect| Microsoft MVP | Community Leader | Speaker | Blogger | Photographer