How to get multiple tables using Entity Framework Core
How to get multiple tables using Entity Framework Core
700
05-Sep-2023
Updated on 06-Sep-2023
Aryan Kumar
06-Sep-2023To get multiple tables using Entity Framework Core, you can use the
DbSetclass. The classDbSetrepresents a collection of entities from a single table. You can load related entities from another table into the current entity using the include method.For example, the following code gets all the customers and their orders in one LINQ call:
C#
This code will first loads all the customers from the
Customerstable. Then, load all the orders for each customer from theOrderstable.You can also use the
Includemethod to load multiple related entities in one LINQ call. For example, the following code gets all the customers, their orders, and the products that they ordered in one LINQ call:C#
This code will first loads all the customers from the
Customerstable. Then, load all the orders for each customer from theOrderstable. Finally, all products ordered by each customer are loaded from theProductstable.Here are some other things to keep in mind when getting multiple tables using Entity Framework Core:
Whereoperator to filter the query results.OrderByoperator to sort the result of your query.TakeandSkipoperators to paginate the query results.