CRUD Operations Without Writing Code In ASP.NET MVC
Introduction
This article explains how to perform CRUD database operations from .NET application without writing any code. This is possible by strong features provided by Microsoft in ASP.NET MVC. You just need to follow the steps given below.
Background
Before starting with this article, we must have knowledge of database and MVC architecture. Here we are going to use database first approach. If you don't have knowledge of database, MVC architecture and database first approach, please learn these first.
Steps
Step 1. First create a database and table tblData. In this table Id field is primary key with auto increment.
Step 2. Open Visual Studio, Click on File - New Project - ASP.NET MVC4 Application. Name your project CrudOperationsWithoutCode or anything else. Click OK.
Step 3. In the next window select Internet Application template and click OK,
Step 4. Right Click in your solution. Click Add - New Item. A popup window will appear.
Step 5. Click ADO .NET Entity Data Model. Name it CrudEntities. Click Add, next window will appear.
Step 6. Select EF Designer from Database. Click Next
Step 7.
In the next window click New Connection, a popup window will appear. Fill the required entries to connect to database. Test your connection and click OK.
Step 8. Check Save connection settings in Web.Config as : and assign a meaningful name to it. Click Next,
Step 9. In the new window select Entity Framework 5.0 and click Next.
Step 10. In the next window expand Tables - dbo – tblData and click Finish.
Step 11.
Build your solution by right clicking in solution and clicking Build. (This is necessary step because if you don't build, it will not show your model class while adding controller)
Step 12. Right click in Controller folder. Click Add-Controller. New popup window will appear.
Step 13. Give a meaningful name to controller. I've given CrudController Under Scaffolding option.
Template : MVC controller with read/write actions and views, using Entity Framework.
Model Class : tblData (CrudOperationsWithoutCode)
Data context class : CRUDDBEntities (CrudOperationsWithoutCode)
Views: Razor (CSHTML) and click Add
Step 14.
All your CRUD operation code done automatically at this stage. Now your code is ready to execute. Just Press F5 or click on execute icon as shown in screen.
Output 1.
This will redirect you to Index view of HomeController. We have to hit Index view of CrudController. To do this change url at you browser as localhost:**** */Crud/Index.
Here you can see all records of the database are being displayed in the screen. So Read operation has been performed. Also you can see there are options like Create New , Edit, Details and Delete.
Output 2. To perform Create operation just click Create New. This will redirect you to localhost:**** */Crud/Create. You have a form to perform Create operation in database from MVC application
Fill the form and click Create. It will save your data to database.
Output 3. Similarly, you can perform Update and Delete operations by clicking on Edit and Deleteoptions in Index view of CrudController.
Points of Interest
Here you can see I've not written even a single line of code and I used powerful features of Scaffolding options provided by Microsoft and performed all create, read, update and delete operation in database from a .NET web application.
No comments:
Post a Comment