Entity Framework Core Intermediate

CRUD in an MVC app with EF Core

CRUD means Create, Read, Update and Delete, the four operations almost every business screen needs. This lesson builds a complete products controller with EF Core and shows how each MVC action maps to a database operation.

CRUD in MVC with EF Core
  1. CreateGET and POST Create: _db.Add(product) then SaveChangesAsync()
  2. ReadIndex and Details: ToListAsync() and FindAsync(id)
  3. UpdateGET and POST Edit: load, change properties, SaveChangesAsync()
  4. DeleteGET Delete to confirm, POST DeleteConfirmed: Remove then SaveChangesAsync()

GET actions show pages; POST actions change data and then redirect.

The idea in plain words

Each operation has a GET action that shows a page and, for changes, a POST action that does the work. The POST action changes entities through the DbContext and calls SaveChangesAsync, which sends the INSERT, UPDATE or DELETE to the database. Visual Studio and the dotnet aspnet-codegenerator tool can scaffold this whole controller and its views from an entity, which is a great way to study the pattern.

Example: the controller

Edit loads the existing entity and copies only the fields the form may change, which prevents over-posting.

C#
public class ProductsController : Controller
{
    private readonly ShopContext _db;
    public ProductsController(ShopContext db) => _db = db;

    public async Task<IActionResult> Index()
        => View(await _db.Products.AsNoTracking().ToListAsync());

    public async Task<IActionResult> Details(int id)
        => await _db.Products.FindAsync(id) is { } p ? View(p) : NotFound();

    public IActionResult Create() => View();

    [HttpPost, ValidateAntiForgeryToken]
    public async Task<IActionResult> Create(ProductForm form)
    {
        if (!ModelState.IsValid) return View(form);
        _db.Products.Add(new Product { Name = form.Name, Price = form.Price });
        await _db.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }

    [HttpPost, ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(int id, ProductForm form)
    {
        var product = await _db.Products.FindAsync(id);
        if (product is null) return NotFound();
        if (!ModelState.IsValid) return View(form);
        product.Name = form.Name;
        product.Price = form.Price;
        await _db.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }

    [HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
    public async Task<IActionResult> DeleteConfirmed(int id)
    {
        var product = await _db.Products.FindAsync(id);
        if (product is not null)
        {
            _db.Products.Remove(product);
            await _db.SaveChangesAsync();
        }
        return RedirectToAction(nameof(Index));
    }
}

In Visual Studio 2026

Visual Studio can write this whole controller and its views for you, which is the fastest way to see CRUD working.

  • Right-click the Controllers folder and choose Add, then New Scaffolded Item.
  • Pick MVC Controller with views, using Entity Framework and click Add.
  • Choose your Model class (Product) and DbContext class (ShopContext), keep Generate views ticked and click Add.
  • Visual Studio adds the NuGet packages it needs, creates ProductsController and the Index, Details, Create, Edit and Delete views.
  • Run the app and open /products. Then read the generated code; it follows the same pattern as this lesson.

Common mistakes

  • Deleting with a GET request, which lets a link or a crawler delete data. Use a POST with a confirmation page.
  • Binding the form straight to the entity and calling Update, which can overwrite fields the form did not show.
  • Forgetting to handle a missing id and returning NotFound.
  • Ignoring DbUpdateConcurrencyException when two users edit the same row; add a concurrency token for important data.

Interview question

Why should a Delete action in MVC use POST instead of GET?

GET requests are meant to be safe and are triggered by links, prefetching, crawlers and browser history, so a GET that deletes data can be fired accidentally or by a malicious page. Using a GET to show a confirmation page and a POST protected by an anti-forgery token to perform the delete makes the action deliberate and protects it against CSRF.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.