Retrieving Person Details from Database using Entity Framework in C#

Hi, I'm Jaimin, a front-end developer with counting years of experience in HTML5, CSS3, JavaScript, and React. I am passionate about creating user-friendly web experiences that look great and drive engagement. In my free time, I enjoy exploring new hiking trails and checking out local coffee shops. Let's work together to bring your web project to life!
Title: Retrieving Person Details from Database using Entity Framework in C#
Introduction: In this blog article, we will focus on understanding and explaining the GetPerosonDetail method, which retrieves a list of person details from the database using Entity Framework in C#. The method will leverage LINQ to Entities to fetch data and will return a custom model containing both basic information and address details.
Understanding the GetPerosonDetail Method: Let's break down the code of the GetPerosonDetail method and understand each step:
public List<PersonDetailModal> GetPerosonDetail()
{
using (var context = new CrudApplicatonEntities())
{
var result = context.PersonDetail.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
}
}).ToList();
return result;
}
}
public List<PersonDetailModal> GetPerosonDetail(): The method signature indicates that this function returns a list ofPersonDetailModalobjects.using (var context = new CrudApplicatonEntities()): The code utilizes ausingstatement to create an instance of theCrudApplicatonEntitiesdatabase context. This ensures that the context is properly disposed of after its use.var result =context.PersonDetail.Select(...): The method utilizes LINQ to Entities to query thePersonDetailtable from the database.Select(x => new PersonDetailModal() { ... }): TheSelectmethod projects eachPersonDetailentity in the database to a new instance of the custom modelPersonDetailModal.Id =x.Id, FirstName = x.FirstName, LastName = x.LastName, Email =x.Email: The properties of thePersonDetailentity are mapped to the corresponding properties of thePersonDetailModalobject.Address = new AddressDetailModal() { ... }: TheAddressproperty of thePersonDetailModalobject is mapped to a new instance of the custom modelAddressDetailModal.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 theAddressDetailentity are mapped to the corresponding properties of theAddressDetailModalobject.ToList(): The query is executed using theToList()method, which returns the result as a list ofPersonDetailModalobjects.return result;: The list ofPersonDetailModalobjects is returned as the output of the method.
Conclusion: The GetPerosonDetail method retrieves person details from the database using Entity Framework and LINQ to Entities. It returns a list of custom model objects (PersonDetailModal) containing both basic information and address details. This method serves as a foundation for fetching data from the database and can be further expanded or integrated into a broader application for displaying, processing, or manipulating person information.






