---
title: "How do perform CRUD (Create, Read, Update, Delete) operations using Entity Framework in MVC?"  
description: "How do perform CRUD (Create, Read, Update, Delete) operations using Entity Framework in MVC?"  
author: "Revati S Misra"  
published: 2023-06-11  
updated: 2023-06-14  
canonical: https://www.mindstick.com/forum/158713/how-do-perform-crud-create-read-update-delete-operations-using-entity-framework-in-mvc  
category: "asp.net mvc"  
tags: ["asp.net mvc", "database connection"]  
reading_time: 3 minutes  

---

# How do perform CRUD (Create, Read, Update, Delete) operations using Entity Framework in MVC?

How do perform [CRUD](https://www.mindstick.com/articles/12244/crud-operation-in-extjs) (Create, Read, [Update](https://www.mindstick.com/forum/156353/what-is-hummingbird-update), [Delete](https://www.mindstick.com/forum/33620/how-to-delete-record-from-list-in-mvc-using-ajax)) [operations](https://www.mindstick.com/blog/304985/how-does-devops-bridge-the-gap-between-development-and-operations-teams-like-git) using [Entity Framework](https://www.mindstick.com/articles/1566/crud-operations-using-entity-framework-code-first-approach) in [MVC](https://www.mindstick.com/forum/155803/define-cache-profile-in-mvc)?

## Replies

### Reply by Aryan Kumar

To perform CRUD (Create, Read, Update, Delete) operations using [Entity](https://www.mindstick.com/forum/160562/database-connectivity-using-entity-framework-database-first-approach) [Framework](https://www.mindstick.com/forum/34615/software-framework-vs-library) in MVC, you can use the following steps:

1. **Create a new ASP.NET MVC application.**
2. **Add Entity Framework to your project.**
3. **Create a model class that represents your database table.**
4. **Create a data context that connects to your database.**
5. **In your controller, create a method for each CRUD operation.**
6. **In your views, use the Entity Framework to bind data to your views.**

Here is an example of how to create a new ASP.NET MVC application using Entity Framework:

Code snippet

```plaintext
dotnet new mvc
```

This will create a new ASP.NET MVC application with the default name "MyApp".

Next, you need to add Entity Framework to your project. You can do this by opening the Package Manager Console and running the following command:

Code snippet

```plaintext
PM> Install-Package EntityFramework
```

This will install the Entity Framework NuGet package into your project.

Now, you need to create a model class that represents your database table. You can do this by creating a new class in your project and adding the following code:

Code snippet

```plaintext
public class Customer
{
    public int CustomerID { get; set; }
    public string CustomerName { get; set; }
    public string Address { get; set; }
}
```

This creates a model class called "Customer" with three properties: CustomerID, CustomerName, and Address.

Next, you need to create a data context that connects to your database. You can do this by creating a new class in your project and adding the following code:

Code snippet

```plaintext
public class MyContext : DbContext
{
    public DbSet<Customer> Customers { get; set; }
}
```

This creates a data context called "MyContext" with a DbSet called "Customers". The DbSet class represents a collection of entities in the database.

Now, you can create a method in your controller for each CRUD operation. For example, here is a method for creating a new customer:

Code snippet

```plaintext
public ActionResult CreateCustomer(Customer customer)
{
    if (ModelState.IsValid)
    {
        _context.Customers.Add(customer);
        _context.SaveChanges();

        return RedirectToAction("Index");
    }

    return View(customer);
}
```

This method accepts a Customer object as a parameter and saves it to the database. If the ModelState is valid, the method redirects the user to the Index action.

Finally, you need to use the Entity Framework to bind data to your views. You can do this by using the ViewModel property in your view. For example, here is a view that displays a list of customers:

Code snippet

```plaintext
@model MyApp.Models.Customer

<table>
    <thead>
        <tr>
            <th>Customer ID</th>
            <th>Customer Name</th>
            <th>Address</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var customer in Model.Customers)
        {
            <tr>
                <td>@customer.CustomerID</td>
                <td>@customer.CustomerName</td>
                <td>@customer.Address</td>
            </tr>
        }
    </tbody>
</table>
```

This view binds the CustomerViewModel class to the Customers view model. The Customers view model is a class that represents a collection of Customer objects.

This is just a basic example of how to perform CRUD operations using Entity Framework in MVC. For more information, you can refer to the Entity Framework documentation.


---

Original Source: https://www.mindstick.com/forum/158713/how-do-perform-crud-create-read-update-delete-operations-using-entity-framework-in-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
