How to Read Record from Database in ASP.MVC Core using Entity Framework Core 1.0
How to Read Record from Database in ASP.MVC Core using Entity Framework Core 1.0
454
05-Sep-2023
Updated on 06-Sep-2023
Aryan Kumar
06-Sep-2023To read a table in asp.net core using the database-first appraoch, follow these steps:
DbContext.DbSet.Here's is an example that shows how a table is read a record from a database in ASP.NET MVC Core using a database-first approach:
C#
In this example, we first create a DbContext class called
MyContext. TheMyContextclass has a property calledCustomersof typeDbSet. This property represents theCustomerstable in the database.Next, create a controller called
CustomerController. TheCustomerControllerclass has a method calledIndex(). TheIndex()method gets the record with the ID of 1 from theCustomerstable in the database and returns it to the view.The view then binds the data from the controller to the UI.
Here are some other things to keep in mind when reading records from a database in ASP.NET MVC Core using Entity Framework Core 1.0:
Findmethod to get a single record from the database.ToList()method to get a list of records from the database.Wheremethod to filter the results of the query.OrderBymethod to order the results of the query.