---
title: "How to read tables in asp.net core, with database first approach"  
description: "How to read tables in asp.net core, with database first approach"  
author: "Utpal Vishwas"  
published: 2023-09-05  
updated: 2023-09-06  
canonical: https://www.mindstick.com/forum/159843/how-to-read-tables-in-asp-dot-net-core-with-database-first-approach  
category: "asp.net core"  
tags: ["database table", "asp.net core", ".net core"]  
reading_time: 2 minutes  

---

# How to read tables in asp.net core, with database first approach

How to read [tables](https://www.mindstick.com/articles/336597/introduction-of-html-tables-for-web-development) in [asp.net core](https://www.mindstick.com/articles/12946/get-started-with-asp-dot-net-core-mvc-and-visual-studio), with [database first approach](https://www.mindstick.com/forum/156731/difference-between-code-base-first-and-database-first-approach)

## Replies

### Reply by Aryan Kumar

To read a table in [ASP.NET](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) Core using the [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax)-first [approach](https://www.mindstick.com/interview/985/what-are-the-approaches-that-you-will-follow-for-making-a-program-very-efficient), follow these steps:

1. Create a DbContext class that inherits from `DbContext`.
2. In your DbContext class, add a property for each table that you want to read. The property should be of type `DbSet`.
3. Create a controller that exposes display the data to user. Your controller should have a method to retrieve data from the DbContext class.
4. In the view, bind the controller data to the UI.

Here is an example of how to read a table in ASP.NET Core with database first approach:

C#

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

public class CustomerController : Controller
{
    private readonly MyContext _context;

    public CustomerController(MyContext context)
    {
        _context = context;
    }

    public ActionResult Index()
    {
        var customers = _context.Customers.ToList();
        return View(customers);
    }
}
```

In this example, we first create a DbContext class named `MyContext`. The class `MyContext` has a property called `Customers` of type `DbSet`. This property represents the `Customers` table in the database.

Then we create a controller named `CustomerController`. The class `CustomerController` has a method called `Index()`. The `Index()` method gets the data from the `Customers` table in the database and returns it to the view.

The view then binds the data from the controller to the UI.


---

Original Source: https://www.mindstick.com/forum/159843/how-to-read-tables-in-asp-dot-net-core-with-database-first-approach

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
