Fetching Individual Person Details from Database using Entity Framework in C#

Fetching Individual Person Details from Database using Entity Framework in C#

Title: Fetching Individual Person Details from Database using Entity Framework in C#

Introduction: In this article, we will focus on understanding and explaining the PersonIndividualDetail method, which retrieves the individual details of a person from the database using Entity Framework in C#. This method is designed to fetch a specific person's information based on the provided id. We will utilize LINQ to Entities to construct the database query and return the relevant data in a custom model that includes both the person's basic details and their address information.

Understanding the PersonIndividualDetail Method: Let's dissect the code of the PersonIndividualDetail method and comprehend each step:

public PersonDetailModal PersonIndividualDetail(int id)
{
    using (var context = new CrudApplicatonEntities())
    {
        var resultIndividualDetail = context.PersonDetail
                                    .Where(x => x.Id == id)
                                    .Select(x => new PersonDetailModal()
        {
            Id = x.Id,
            FirstName = x.FirstName,
            LastName = x.LastName,
            Email = x.Email,
            Address = new AddressDetailModal()
            {
                Id = x.AddressDetail.Id,
                Street = x.AddressDetail.Street,
                City = x.AddressDetail.City,
                State = x.AddressDetail.State,
                PostCode = x.AddressDetail.PostCode,
                Country = x.AddressDetail.Country
            }
        }).FirstOrDefault();
        return resultIndividualDetail;
    }
}
  1. public PersonDetailModal PersonIndividualDetail(int id): The method signature indicates that this function returns a single PersonDetailModal object and requires an id parameter to specify which person's details to retrieve.

  2. using (var context = new CrudApplicatonEntities()): The code uses a using statement to create an instance of the CrudApplicatonEntities database context. This ensures that the context is properly disposed of after its use.

  3. var resultIndividualDetail = context.PersonDetail.Where(x => x.Id == id).Select(...): The method employs LINQ to Entities to query the PersonDetail table from the database, filtering the results based on the provided id.

  4. Select(x => new PersonDetailModal() { ... }): The Select method projects the filtered PersonDetail entity to a new instance of the custom model PersonDetailModal.

  5. Id = x.Id, FirstName = x.FirstName, LastName = x.LastName, Email = x.Email: The properties of the filtered PersonDetail entity are mapped to the corresponding properties of the PersonDetailModal object.

  6. Address = new AddressDetailModal() { ... }: The Address property of the PersonDetailModal object is mapped to a new instance of the custom model AddressDetailModal.

  7. Id = x.AddressDetail.Id, Street = x.AddressDetail.Street, City = x.AddressDetail.City, State = x.AddressDetail.State, PostCode = x.AddressDetail.PostCode, Country = x.AddressDetail.Country: The properties of the related AddressDetail entity are mapped to the corresponding properties of the AddressDetailModal object.

  8. FirstOrDefault(): The query is executed using the FirstOrDefault() method, which returns the first matching record from the filtered results. If no record is found, it returns null.

  9. return resultIndividualDetail;: The individual person's details, as a PersonDetailModal object, are returned as the output of the method.

Conclusion: The PersonIndividualDetail method fetches an individual's details from the database based on the provided id using Entity Framework and LINQ to Entities. It returns a custom model (PersonDetailModal) that includes both the person's basic information and their address details. This method can be seamlessly integrated into a broader application for displaying, editing, or processing specific individual details based on the id.