Web
Analytics
CRUD in MVC using Entity Framework Code first and sql - Tutorial

For Consultation : +91 9887575540

Stay Connected :

Entity framework code first SQL:

Most of viewers and visitors of Yogeshdotnet.com, requested to upload some blogs based on entity framework code first with SQL. First of all I want to say thanks to all my visitors and regular viewers who shows their keen interest in my blogs.

Most of native programmers who have experience in traditional ado.net and SQL , they love to work in SQL ,they feel  it pretty easy .Writing Lambda expression for all database operations sometimes become very bigger task.  Entity framework API provides SQL Query function which is used to perform all SQL select queries.

In this example I am going to create a product table on where I will perform crud operations.





Product Table

EntityFramework-Codefirst-SQL-Product-Table

SQL Query for product table

 

 Note: Please insert some dummy data into Product table  to view results .now come to the Visual Studio.





Let’s start

  1. Create a new Project. Open Visual Studio 2013.
  2. Go to “File” => “New” => “Project…”.
  3. Select “Web” in installed templates.
  4. Select “ASP.NET MVC Web Application”.
  5. Enter the Name and choose the location.
  6. Click “OK”.

Step 1:  first we create model class for product table.

 

Step 2:   create data context class for all database operations.

 

Step 3 create product controller

Step 4: Here I am writing action for searching product by product ID.

 Note : Add Empty View with Index Action:





Index.cshtml

press F5 in see output

Entity-Framework-SQL-Search-code-example

 

Step 5: now we will write code to insert products in database table
Right action to add product




AddProduct.cshtml

Entity-Framework-code-first-with-sql-example-AddProduct

Step 6: Now we write action to update product table.

Create view with template create and select product as model class and data context as context class

UpdateProduct.cshtml

Press F5 and see results.

Entity-Framework-Code-First-SQL-Update





Step 7: Now I will write Code to delete product.

ProductDelete.cshtml

Entity-Framework-tutorial-MVC-Delete-Product-Table
Download Complete Source Code




Select your currency