January 27, 2017
CRUD operation MVC 5 Web App with Azure SQL Database.
In the earlier tutorial, I talked about the creating SQL database in Azure and connecting it through SQL Server Management Studio.
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 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 as,
Right-click the Models folder in the Solution Explorer and select Add -> New Item.
Select ADO.NET Entity Data Model and give name to a 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 time to create the Controller as,
Right-click the Controllers folder and select Add -> Controller.
In the Add Scaffold dialog, choose MVC 5 Controller with views, using Entity Framework.
Choose the Model Class, Data Context Class and enter the Controller Name as well.
Click the 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 to the database field you have created to see the data is saved there or not,
If any problem occurred feel free to ask me. I will help you.
That’s all for today. Thanks!