Kacper Gwiazdowski
Kacper Gwiazdowski

Reputation: 141

Connecting ASP.NET Core App to Google Cloud SQL

I have created simple app using Asp.NET Core. It uses entity framework and local database. I deployed my app to Google Cloud App Engine but honestly I have no idea what to do with my database. For now I'm using local sql server and my appSettings.json looks like this:

{
  "connectionStrings": {
    "smogInfoDBConnectionString": "Server=(localdb)\\mssqllocaldb;Database=SmogInfoDB;Trusted_Connection=True",
    "hangfireConnectionString": "Server=(LocalDB)\\MSSQLLocalDB;Integrated Security=true"
  }
}

and it works just fine when I'm debugging and using app on my computer. I understand that I need to create an instance of SQL, but how to connect my app to that database server? How to make connection string?

Upvotes: 5

Views: 3035

Answers (2)

navule
navule

Reputation: 3624

You must Authroize your computer's public ip address on Cloud SQL to access your instance.

Follow these steps:

  • https://checkip.amazonaws.com/ and obtain your PC's IP address.
  • On Cloud Console, navigate to Cloud SQL instance detail -> Networking Tab -> Authorized networks (section) -> Add Network.
  • Optionally, provide Name as something like Kacper's PC Home
  • In the Network* text box which is needs a mandatory IP or CIDR format input, provide your IP adddress obtained from step 1.

Ensure that you have a static ip. Some corporate networks has a config such that the IP changes everytime you reboot or shutdown and start your PC. In such case work with your IT team and add a CIDR range on Cloud SQL Instance.

Upvotes: 1

Alex Riabov
Alex Riabov

Reputation: 9165

Your connection string to Google Cloud SQL should look like:

Server=xxx.xxx.xxx.xxx;Database=DB_NAME;Uid=USER_NAME;Password=PASSWORD

Upvotes: 5

Related Questions