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 ofPersonDetailModal
objects.using (var context = new CrudApplicatonEntities())
: The code utilizes ausing
statement to create an instance of theCrudApplicatonEntities
database 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 thePersonDetail
table from the database.Select(x => new PersonDetailModal() { ... })
: TheSelect
method projects eachPersonDetail
entity 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 thePersonDetail
entity are mapped to the corresponding properties of thePersonDetailModal
object.Address = new AddressDetailModal() { ... }
: TheAddress
property of thePersonDetailModal
object 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 theAddressDetail
entity are mapped to the corresponding properties of theAddressDetailModal
object.ToList()
: The query is executed using theToList()
method, which returns the result as a list ofPersonDetailModal
objects.return result;
: The list ofPersonDetailModal
objects 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.