Deploy an ASP.NET Core App with EF Core and PostgreSQL to Google Cloud

Sena Kılıçarslan
.NET Core
Published in
7 min readAug 15, 2019

In this post, I will show how to deploy an ASP.NET Core web application with EF Core and PostgreSQL to App Engine on Google Cloud Platform (GCP). Besides, I will show how to use Cloud SQL for PostgreSQL instance from this application.

I will use the following tools:

The sections of this post will be as follows:

  • About the App
  • Sign up for Google Cloud
  • Create the GCP Project
  • Cloud SQL
  • Modify the App and Initialize the Database
  • Check the Database (pgAdmin)
  • Test
  • Publish To Google Cloud
  • Clean up the GCP Project

In the previous post, I demonstrated how to deploy an ASP.NET Core web application starter project to GCP. As it has no database access, it is a simpler version of this one and I recommend you start from there if this is your first deployment to GCP.

If you are ready, let’s get started.

About the App

The application that we will deploy manages a database of TV Shows and its main page looks like below:

The app is an ASP.NET Core MVC web application and it uses EF Core to perform CRUD operations on a PostgreSQL database.

You can download the source code from this GitHub repository.

Sign up for Google Cloud

If you don’t have a Google Cloud account, go to Google Cloud web site and click Get started for free button. Sign up with your Gmail account and get $300 (for free) to spend on Google Cloud Platform over the next 12 months.

Creating the GCP Project

Next, go to Google Cloud Console and type App Engine in the search box and click Create in the dashboard of the App Engine to create a new project:

Name your project and then select your region and enable billing in the next page:

Cloud SQL

Now, we will create our PostgreSQL instance in the Cloud SQL.

Cloud SQL is a fully-managed database service that makes it easy to set up, maintain, manage, and administer your relational databases on Google Cloud Platform. You can use Cloud SQL with either MySQL or PostgreSQL.

First, select APIs & Services -> Library

and in the next page search for Cloud SQL API and Enable this API:

Next, search for Cloud SQL and click Go To Cloud SQL in the following page:

Click Create Instance in the next page:

and then choose PostgreSQL.

On the next page, give an ID to your instance and set your password for the default (postgres) user.

We will use this Id and password to connect to this instance later in the tutorial.

Next, the Compute Engine API creates the instance and it takes a few minutes.

After you see the green tick on the instance ID, click on the instance to go to Instance Details page:

In the Overview tab, you can see the IP and connection name for the instance:

Next, go to the Connections tab and click Add Network:

  • Enter all for the name.
  • Enter 0.0.0.0/0 for the network.
  • Click Done, then Save.

Security Issues

As you see, we gave access to public IPs to make login attempts to the instance. SSL encryption is recommended when using Public IP to connect to the instance.

For the sake of simplicity, I will not implement this part in this tutorial. If you want to implement this, please follow the instructions here. Besides, you will need to modify your app too. If you need help in this part, let me know in the comments.

Modify the App and Initialize the Database

In Visual Studio, open the application and modify the connection string in appsettings.json:

"ConnectionStrings": {"TvShowsContext": "Uid=[YOUR_USER_NAME];Pwd=[YOUR_PASSWORD];Host=[YOUR_INSTANCE_IP];Database=TvShowsContext-fe1316f5-48e2-4739-ae44-eccbe217ab10"}

If you haven’t created your migration file already, run the following command in the Package Manager Console:

Add-Migration Initial

and then run the below command to create the database on the instance:

Update-Database

Check the Database (pgAdmin)

Now, we will check the database created on Cloud SQL using pgAdmin.

Launch pgAdmin and then select Servers -> Create -> Server:

and then fill in the fields shown in the red box below:

As you see below, our database and tables were created on the Cloud SQL:

Test

Now, we will test the application on our local computer.

In this test configuration, our application server is on localhost and the database server is on the cloud.

Run the application on the Visual Studio and click TVShowsApp on the web page and then Create New on the next page.

After creating our first record, it is shown below on the main page:

We can check the record created using pgAdmin as well:

Publish to Google Cloud

Now, we will publish our application to Google Cloud.

First, install Google Cloud Tools for Visual Studio if you don’t have it already.

Open Visual Studio and go to Tools -> Extensions and Updates. Search for Google Cloud Tools for Visual Studio and click Install (It’s already installed on my computer):

As a side note, Cloud Tools for Visual Studio is not supported in VS 2019. So, I am using VS 2017 for this tutorial.

Next, we will link our application to our GCP project.

Open Tools -> Google Cloud Tools -> Manage Accounts:

and select Add account. Login to the account that you used to register to the GCP.

You will see your account logged at the top right corner of Visual Studio. There is a No Project label near this account and click that and click Select Project:

Select the project that you created above.

Enable the following APIs from Google Cloud Console:

Next, right-click on the project in Visual Studio and select Publish To Google Cloud. Then select App Engine Flex in the next dialog.

After the deployment operation is completed, the web page is launched automatically and the application’s URL is as follows:

https://[Your-project_id].appspot.com

I created a new record as we did in the previous section and the main page of the application running on the cloud looks like below now:

Again, we can check the new record from the database using pgAdmin:

Clean up the GCP Project

To avoid incurring charges, we can delete our GCP project to stop billing for all the resources used within the project.

In the Google Cloud Console, search for Manage Resources. In this menu, select your project and click Delete:

That’s the end of the post. I hope you found this post helpful and easy to follow. If you have any questions and/or comments, please let me know in the responses section below.

And if you liked this post, please clap your hands 👏👏👏

Bye!

References

https://cloud.google.com/appengine/docs/flexible/dotnet/using-cloud-sql-postgres

--

--

Sena Kılıçarslan
.NET Core

A software developer who loves learning new things and sharing these..