If you are a .NET developer and have been following this series, you may have read the previous section covering Google Cloud Platform’s Cloud SQL as part of the Cloud Storage Options. In that example, I showed how to create and connect to a Cloud SQL instance database using Visual Studio’s Server Explorer. But there’s more to that story! I also want to show how to take that connection and use our Cloud SQL database with Entity Framework in a sample Web application.
Before we create the Entity Framework model, we should add some tables to test with. At this point you can use ANY interface you like to execute the following commands, but my favorite ad-hoc query execution environment is LINQPad! It’s free and I wanted to see how it works with a Cloud SQL instance. Just for the sake of completeness, I’ll show you how to connect LINQPad to the Cloud SQL instance.
Before you start, review the previous post and create your Cloud SQL instance, either using your gcloud shell or the GCP Console.
Before you can connect to LINQPad, you’ll have to add a MySQL driver. If you don’t see a driver in the list that supports MySQL, you can use the “View more drivers” option to install the IQ driver.
Then, create the connection using the IP address of your Cloud SQL instance and the root user password.
Once the connection succeeds, you can create a Query. Among the reasons I love LINQpad is the ability to write queries in C#, but in this case you can choose SQL for the language.
Now execute the following SQL to create a table of survey questions, with a foreign key to a table of question types.
CREATE TABLE IF NOT EXISTS questiontype (
questiontypeID int unsigned NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL DEFAULT '',
PRIMARY KEY (`questiontypeID`)
CREATE TABLE IF NOT EXISTS surveyquestion (
surveyquestionID int unsigned NOT NULL AUTO_INCREMENT,
surveyquestiontext varchar(255) NOT NULL DEFAULT '',
questiontypeID int unsigned NOT NULL,
PRIMARY KEY (surveyquestionID),
FOREIGN KEY (questiontypeID) REFERENCES questiontype (questiontypeID)
INSERT INTO questiontype (name) VALUES
INSERT INTO surveyquestion (surveyquestiontext, questiontypeID) VALUES
('Would you purchase this product again?', 1),
('How would you rate the quality of our product?', 2),
('How would you rate the price of our product?', 2),
('Do you have any comments to add?', 3)
You should now be able to expand the Tables view in LINQpad and see our sample schema.
Add an Entity Framework model
To demonstrate an Entity Framework model created from this Cloud SQL database, we can use an empty WebAPI web application project, which you can download here.
Note: you’ll need to add some MySQL nuget packages if you are creating this project on your own.
Creating the model is very straightforward. Start by adding the ADO.NET model object using the wizard.
And if you have the packages above, you will be able to create the EF Designer model and connect to the Cloud SQL instance just as we did before.
Save the connection and choose to add our tables to the model.
Now we can see our tables in the model, and see that a navigation property has been added to let us navigate from questions to question types.
Great! At this point be sure to build the application. Next, let’s use that model in a very simple test API call.
Add a controller called SurveyQuestionsController.
And in the controller Get method, add the following code which uses the new Entity Framework model classes.
public class SurveyQuestionsController : ApiController
// GET api/<controller>
public HttpResponseMessage Get()
using (var db = new falafelefdemoEntities())
var questions = db.surveyquestions.Select(q =>
Text = q.surveyquestiontext,
TypeName = q.questiontype.name
return Request.CreateResponse(HttpStatusCode.OK, questions);
If you run the application now, you can test this API call by browsing (or using Postman, if you prefer) to the host address + /api/surveyquestions. The response should include the questions plus the names of their question types, which was assembled using our Entity Framework navigation property.
And there you have it! A working Entity Framework model, from Visual Studio, all using Google Cloud Platform’s Cloud SQL instance.