Tuesday 22 December 2020

CRUD Operation Using EF6.0 Code First in Asp.Net MVC

In this article I will focus on CRUD operation using Entity Framework code first, lets understand CRUD (Create, Read, Update, Delete). Well this is important article where I’m going to demonstrate database communication using EF 6.0 Code First. EF provide an API and helps us to write minimal code to communicate with database efficiently and provide code maintainability.


Lets create new project using MVC and name it CRUDOperation




 

Create new class under Models folder and name it as Employee.cs

Add below code in Employee class. Properties (ID, FisrtName,..) from Employee act as column name of table employees. How to create table in database with the help of below class will see soon in this article, but for now lets understand below model represent as table and it’s columns in front end.



Go to root web.config file and add connection string. MVC5CRUD is name used in DbContext class.


Create WebAppContext.cs class in model’s folder.

DbContext class is available in EntityFramework. It act as bridge between your model (entity) classes and the database. It is primary class that is responsible for interacting with the database. Here need to declare model as property of type DbSet<T> to create table in database. Here I used only one model but can use more than one model based on requirement.


To create table in database for highlighted property, need to perform few commands from package manager console.

Before executing command from package manager console make sure EntityFramework 6.0 is install for respective project.

 

Open package manager console from tools.

 

Provide command to package manager console: Enable-Migration


Now pass command ‘Add-Migration IntialDb’ to package manager console. It create folder Migrations and create few classes under migration folder.

 

 

Below class from Migration folder keeps track of tables need to create in actual database in Up method and Drop/Delete table in Down method highlighted.


Now again provide Update-Database command in package manager console to update all changes to actual database


Tables gets created in MS SQL server, but observe for employee model  Employees table created and one more table with name __MigrationHistory.

The use of this table is to maintain history of each migration. It create new row as history when you provide Update-Database command from Package manager console.


 

I have updated my database twice using Update-Database command, you can update your Database any time and n-number of times based on modification in your model classes, name of property add/update inside model or new model gets added etc.

 

Now everything set up for CRUD Operation using MVC. Now create Employee controller to implement CRUD operation related to Employee model.

 Update Index action code.



Create View for Index action method by using right click on Index and select Add View… option.



If Employee class is not visible in Model Class field for below screen, build solution and try again to create Index view.

 

View gets created. One important thing while creating view, explicitly mentioned model class. It allows view to be created as specific type view for Employee model and we can access properties of employee model in view to show data to end user. The code is automatically generated using ASP.NET MVC Scaffolding. You observe under Views->Employee Index.cshtml get created with below code.





Now run application and check Index action of Employee controller.




Next article will discuss on Create, Update and Delete operation using EF Part-2

                          

                              


0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home