Tuesday, September 24, 2019

EF Core joining tables using Include and selecting specific columns

In the previous posts we saw on how to join tables using Include and how to select specific columns form a table. In this post we will try to combine both of them, we will join tables using Include and select specific columns from the joined tables.

We will join the Employee table with Department and Country tables, select specific columns from each of the table and populate the below Model class.

EF Core select specific columns in a table

In the previous posts for CRUD operations we saw on how to select all the column values from a context table, sometimes we will not need all the columns from a table instead we will need only specific columns from a table. In these cases we can use a Select () clause to filter specific columns from a table. In this post we shall see on how to select only the Id, Name and Age properties from the employee table.

The employee table has the following columns. 

Sunday, September 8, 2019

EF Core Joining tables with Include

In the previous pose we saw on how to join tables using the join keyword. Entity Framework core also provides an alternate way to join tables using the Include clause.  To use include for joining the tables we need to set the Primary Key, Foreign Key relation in the Database. 

In the sample we have an Employee table which has a Foreign Key DepartmentId referencing to the Department table. We will join these tables using Entity Framework Core.

Saturday, September 7, 2019

Asp.Net EF Core Joining multiple tables

In the previous post we creates a custom model class EmployeeDetail, in this post we shall see on how to use Entity Framework Core to JOIN the Employee, Department and Country tables and populate data into the custom Model class EmployeeDetail.

We have 3 tables, the Employee table is the main table which has related data in the Department and Country tables, DepartmentId and CountryId are the Foreign Keys in the employee table.

Custom Models in Asp.Net Core

So far we have seen CRUD operations using a single table, in real time scenarios we will have to deal with data from multiple tables, like joining multiple tables and returning specific columns from each of the tables. The default Models created using Entity Framework Core will not help in these cases.

For example let us consider the following Employee table, which has references to Department and Country tables.