Using ASP.NET MVC and Entity Framework, you can create a web application that provides an interface to database and allows you to perform basic CRUD operations (Create, Display, Edit and Delete).
We are going to create a demo database in SQL Server 2012 and use Entity Framework Database First approach to interface with the demo database.
In this tutorial, you:
- Create a Sample Database in SQL Server
- Use ADO.NET to Connect to the SQL Server Database
- Create view page to display all the records
- Create page to insert record to the database
- Create page to update record
- Delete record
- Sorting, Filtering and Paging
Prerequisites
- Visual studio 2017
- SQL server 2012
Create a Sample Database in SQL
Server
Launch Visual Studio (in my case VS 2017) and create a new MVC project.Click Ok and wait for visual studio to complete the process
Go to Server Explorer and right click on the Data Connections, select create New SQL Server Database. Complete the dialogue as shown:
Let us add Student table. Expand the new database created, right click on Tables, select add new table.
Modify the table designer to achieve below:
Right-Click on the "Id" row, go to properties and set the Identity Specification to true as shown.
Change the table name in the generated SQL Script to Student.
Use ADO.NET to Connect to the SQL
Server Database
Open
Solution Explorer, add DataAccessLayer folder to Models. Right click on the DataAccessLayer
folder and select add new item.
Click on Add,
on the pop up dialogue, select EF Designer from data… and click next.
Make sure
your next dialogue looks like the one below and click new
On the next
dialogue, select Entity Framework 6.x and click next
Check the
tables and pick the student table as show.
Click on
finish to complete the process.
EF will
generate a layer on your database and exposes tables you specified in the
database as objects which we can work with in the application. EF generated a
context class which inherit from DbContext.
The
generated context class:
MVCSampleCRUD_DBEntities
class will be used to perform database manipulations as described later in this
tutorial.
Now that we
have successfully added database layer to our application, we will now focus on
creating MVC pages to interact with the database in the next article.
Implementing CRUD Operation With Entity Framework in Asp.net MVC
Reviewed by Akintunde Toba
on
March 01, 2019
Rating:
No comments: