Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
Download code here: https://code.msdn.microsoft.com/ASPNET-Connect-to-MySQL-ec6379d4
STEP 1 - Create Azure Account
You need to get a Microsoft Azure account. Everyone can open an Azure account for free.
Check the link below for more information.
http://www.windowsazure.com/en-us/pricing/free-trial/
STEP 2 - Create MySQL Database on Azure
After getting access to an 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.
https://code.msdn.microsoft.com/site/view/file/180287/1/mysql_1.png
After creating the database, we need to get the connection string that will be used on Web Application to access the Azure Database.
For that, select the database created and on the main window, on the right side, we have an option called "Show Connection String".
When we select that option, a new tab will appear, like the following image, with the connection string formatted to different providers.
https://code.msdn.microsoft.com/site/view/file/180288/1/mysql_2.png
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 on the image below.
https://code.msdn.microsoft.com/site/view/file/180289/1/mysql_3.png
- Press OK, and a new screen will appear, with several options of the template to use on our project.
- Select MVC.
https://code.msdn.microsoft.com/site/view/file/180290/1/mysql_4.png
After selection of our template, your first web application using ASP.NET is created.
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 on the solution and choose the option Class. Create the class like the one on the image above.
https://code.msdn.microsoft.com/site/view/file/180291/1/mysql_5.png
We need to validate that EntityFramework and MySQL.Data.Entity is installed like we saw on the image below.
https://code.msdn.microsoft.com/site/view/file/180292/1/mysql_6.png
STEP 5 - Scaffolding
This could be made easily 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.
https://code.msdn.microsoft.com/site/view/file/180293/1/mysql_7.png
Select the name of the controller, class model and data context class.
https://code.msdn.microsoft.com/site/view/file/180294/1/mysql_8.png
The new controllers and views associated were created with success.
https://code.msdn.microsoft.com/site/view/file/180295/1/mysql_9.png
STEP 6 - Change Connection String
https://code.msdn.microsoft.com/site/view/file/180296/1/mysql_10.pnghttps://code.msdn.microsoft.com/site/view/file/180297/1/mysql_11.png
STEP 7 - Change Menu Layout
To test the new entity created, we can add one new entry in the web application menu.
https://code.msdn.microsoft.com/site/view/file/180298/1/mysql_12.png
STEP 8 - Run Application
Press now the F5 button, to run the web application.
The new entities appear on the menu.
https://code.msdn.microsoft.com/site/view/file/180299/1/mysql_13.png
Press the option car to see our entity in action.
https://code.msdn.microsoft.com/site/view/file/180300/1/mysql_14.png