Certainly! Let’s go through each step of CRUD operations in ASP.NET Core MVC with Entity Framework Core using an example of a simple “Product” entity.

Step 1: Setting up the Database Context:

Create a new class named AppDbContext that derives from DbContext in your project:

using Microsoft.EntityFrameworkCore;

public class AppDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }

    // Other DbSet properties and configuration can be added here

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // Configure the database provider and connection string
        optionsBuilder.UseSqlServer("your_connection_string_here");
    }
}

In this example, we have a DbSet<Product> property called Products to represent the “Product” entity in the database. You can add additional DbSet properties for other entities as needed.

Read Also: How to set up your DbContext (in detail)

Step 2: Create Operation (Create):

In your controller, inject an instance of the AppDbContext class via dependency injection:

public class ProductController : Controller
{
    private readonly AppDbContext _context;

    public ProductController(AppDbContext context)
    {
        _context = context;
    }

    // Other action methods and controller logic
}

In the appropriate action method, create a new instance of the “Product” entity, set its properties with the data you want to save, and add it to the Products property of the context:

[HttpPost]
public IActionResult Create(Product product)
{
    _context.Products.Add(product);
    _context.SaveChanges();

    return RedirectToAction("Index");
}

This example demonstrates the Create action method that receives a Product object via a form submission. It adds the product to the Products property of the context and calls SaveChanges to persist the changes to the database.

Step 3: Read Operation (Retrieve):

In your controller, inject an instance of the AppDbContext class via dependency injection:

public class ProductController : Controller
{
    private readonly AppDbContext _context;

    public ProductController(AppDbContext context)
    {
        _context = context;
    }

    // Other action methods and controller logic
}

In the appropriate action method, retrieve the data from the database using the Products property of the context:

public IActionResult Index()
{
    var products = _context.Products.ToList();

    return View(products);
}

This example shows the Index action method that retrieves all the products from the database using the ToList method, which converts the query result into a list of products. The products are then passed to the view for display.

Step 4: Update Operation (Update):

In your controller, inject an instance of the AppDbContext class via dependency injection:

public class ProductController : Controller
{
    private readonly AppDbContext _context;

    public ProductController(AppDbContext context)
    {
        _context = context;
    }

    // Other action methods and controller logic
}

In the appropriate action method, retrieve the entity you want to update from the database using the Products property of the context:

[HttpPost]
public IActionResult Update(Product product)
{
    var existingProduct = _context.Products.Find(product.Id);
    if (existingProduct == null)
    {
        return NotFound();
    }

    existingProduct.Name = product.Name;
    existingProduct.Price = product.Price;

    _context.SaveChanges();

    return RedirectToAction("Index");
}

This example demonstrates the Update action method that receives a Product object via a form submission. It finds the existing product by its ID using the Find method,

updates its properties, and then calls SaveChanges to persist the changes to the database.

Step 5: Delete Operation (Delete):

In your controller, inject an instance of the AppDbContext class via dependency injection:

public class ProductController : Controller
{
    private readonly AppDbContext _context;

    public ProductController(AppDbContext context)
    {
        _context = context;
    }

    // Other action methods and controller logic
}

In the appropriate action method, retrieve the entity you want to delete from the database using the Products property of the context:

[HttpPost]
public IActionResult Delete(int id)
{
    var product = _context.Products.Find(id);
    if (product == null)
    {
        return NotFound();
    }

    _context.Products.Remove(product);
    _context.SaveChanges();

    return RedirectToAction("Index");
}

This example shows the Delete action method that receives the ID of the product to delete. It finds the product by its ID using the Find method, removes it from the Products property of the context, and then calls SaveChanges to persist the deletion to the database.

Remember to replace “your_connection_string_here” in the OnConfiguring method of the AppDbContext class with your actual database connection string.

These are the basic steps to perform CRUD operations in ASP.NET Core MVC with Entity Framework Core.