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;
}
}
public PersonDetailModal PersonIndividualDetail(int id)
: The method signature indicates that this function returns a singlePersonDetailModal
object and requires anid
parameter to specify which person's details to retrieve.using (var context = new CrudApplicatonEntities())
: The code uses ausing
statement to create an instance of theCrudApplicatonEntities
database context. This ensures that the context is properly disposed of after its use.var resultIndividualDetail = context.PersonDetail.Where(x =>
x.Id
== id).Select(...)
: The method employs LINQ to Entities to query thePersonDetail
table from the database, filtering the results based on the providedid
.Select(x => new PersonDetailModal() { ... })
: TheSelect
method projects the filteredPersonDetail
entity to a new instance of the custom modelPersonDetailModal
.Id =
x.Id
, FirstName = x.FirstName, LastName = x.LastName, Email =
x.Email
: The properties of the filteredPersonDetail
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 the relatedAddressDetail
entity are mapped to the corresponding properties of theAddressDetailModal
object.FirstOrDefault()
: The query is executed using theFirstOrDefault()
method, which returns the first matching record from the filtered results. If no record is found, it returnsnull
.return resultIndividualDetail;
: The individual person's details, as aPersonDetailModal
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
.