Saturday, September 7, 2019

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.



Let us say we want an API Action method to return details like EmployeeId, Department Name, Name, Age and Country Name, then the default Entity Framework Core models cannot be used, we will have to create a custom Model as follows.

    public class EmployeeDetail
    {
        public int EmployeeId { get; set; }
        public string Department { get; set; }
        public string Name { get; set; }
        public int? Age { get; set; }
        public string Country { get; set; }
    }

In the next post we shall see on how to join multiple tables using Entity Framework Core and populate this Model class.

Search Flipkart Products:
Flipkart.com

No comments: