CRUD Operations In MVC 5 Web App With Azure SQL Database

In the earlier tutorial, I talked about creating a SQL database in Azure and connecting it through SQL Server Management Studio.

Link: Creating SQL Database in Azure and Connecting through SSMS

Now, this tutorial is to perform CRUD operation on Azure SQL Database through MVC 5 Web App. First of all, create a table inside the database. I have a table "tbl_info" which holds:
  • Id primary key
  • Name nvarchar,
  • Address nvarchar.
 

Then, I am using a database first approach here. So, I am creating the Model from the database. Create an ASP.NET MVC Web application in Visual Studio, as shown below.



 

Create a Data Model.

Right-click the "Models" folder in the Solution Explorer and select Add -> New Item. Select ADO.NET Entity Data Model and give name to this model. Then, click Add.
 
 

Choose EF Designer for database as a model from Entity Data Model wizard. Then, click "Next". Click "New Connection" on next window.
  • Enter the Database Server name.
  • Choose SQL Server Authentication.
  • Enter the username and password of the Database Server.
  • Enter the Database name that you created. In my case, it is "data-to-azure-sql".
Test your database connection and click "OK". Now, your Entity Data Model wizard should look like this.



Provide the connection string name and click "Next".
In the next step, you need to choose the objects for your model. Make sure to choose the table that you created earlier.
Provide the namespace and then click on "Next".
Then, build the project once.

Now, it is time to create the Controller. 

Right-click the "Controllers" folder and select Add -> Controller.

In the "Add Scaffold" dialog, choose "MVC 5 Controller with views, using Entity Framework" option.

Choose the Model Class, Data Context Class, and enter the Controller Name as well.

Click on Add.

 

Go to the App_Start Folder and inside "RouteConfig.cs" change the Controller name "Home" to the Controller name that you entered earlier. It will help redirect you directly towards the particular Controller Operations.

Finally, run it into the browser. Now, you are able to Create, Read, Update, Delete the records.
 

Also, look at the database field you have created, to check if the data is saved there or not.

 

If any problem has occurred, please feel free to ask me. I will help you.

That's all for today. Thanks!
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now