In Focus

ASP.NET - Connect to MySQL Azure Database

Using MVC, Entity Framework, ASP.NET Scaffolding, and Azure MySQL you can create a web application that stores your information on an MongoDB Azure database. This demo shows you how to create a web application with MVC and Entity Framework 7, that communicate with a MySQL Azure database.

Introduction

Using MVC, Entity Framework, ASP.NET Scaffolding, and Azure MySQL, you can create a web application that stores your information on a MongoDB Azure database. This demo shows you how to create a web application with MVC and Entity Framework 7, that communicates with a MySQL Azure database. 

STEP 1 - Create Azure Account

You need to get a Windows Azure account. Everyone can open a Windows Azure account for free. Check the link below for more information.

STEP 2 - Create MySQL Database on Windows Azure

After getting access to the Azure Account, we need to create a MySQL Database to store your data.

So for that, we need to select the option "New" on the left bottom of our web page and then, select the option Data + Storage -> MySQL Database-> set the name and provide the configurations you need.

Azure Database

After creating the database, we need to get the connection string that will be used in the web application to access the Azure Database. For that, select the database created and on the right side of the main window, we have an option called "Show Connection String".

When we select that option, a new tab will appear, like in the following image, with the connection string formatted to different providers.

Azure Database

STEP 3 - Create ASP.NET Web Application

Go to Visual Studio’s File >> New Project menu, expand the Web category, and pick ASP.NET Web Application, like in the image below.

Azure Database

  • Press OK, and a new screen will appear with several options for a template to use in our project.
  • Select MVC here.

    Azure Database

After selection of our template, your first web application is created using ASP.NET.

STEP 4 - Create Data Model

After we have our web application created, we need to create our data model.

For that, select the option "Add New Item" o the solution and choose the option "Class". Create the class like in the image below. 

Azure Database

We need to validate that EntityFramework and MySQL.Data.Entity are installed.

Azure Database

STEP 5 - Scaffolding

This could be made easy using the Scaffolding functionality. On the solution on the top of Controller folder, select the option "Add New Scaffold Item".

On the new screen, select the option MVC6 Controller with views using Entity Framework.

Azure Database

Select the name of the Controller, Model class, and the Data Context class.

Azure Database

The new Controllers and Views associated are created with success.

Azure Database

STEP 6 - Change Connection String

Azure Database

Azure Database

STEP 7 - Change Menu Layout

To test the new entity created, we can add one new entry on the web application menu.

Azure Database

STEP 8 - Run Application

Press the F5 button to run the web application. The new entity appears on the menu.


Azure Database

Press the option "ca"r to see our entity in action.

Azure Database