- by gowthamk91
Introduction:
In this series till now we done a lot of work on interacting with simple objects, now it’s time to interacting with related data. It’s always quite interesting while dealing with related data in Entity Framework Core, in this blog you will learn about how to handle/querying the related data in EF Core using Eager loading and Query Projection.
There are four method which are used to query the related data in EF Core.
- Eager Loading
- Query Projection
- Explicit loading
- Lazy Loading
This is a continuation of my last blog
Inserting Related Data:
Before jumping into Eager loading will see how to insert the related data in EF Core, in part III of this series we implemented one to many relationships between User and Rating entities. Now I’m trying to insert the rating related data in Users entity.
public static void AddUserWithRating()
{
var user = new User
{
Name = "Saravanan",
Rating = new Rating {RatingLevel="A"}
};
context.Users.Add(user);
context.SaveChanges();// Saves all changes made in this context to the database
}
From the above code you can observe the user object with Rating data is added to the Users entity.
public static void AddRatingtoExistingUsers()
{
var user = context.Users.ToList();
context.Ratings.Add(new Rating
{
RatingLevel = "B",
Users = user
});
context.SaveChanges();
}
The above method will be used to add the new Rating object and update it for all the Users in the context.

From the above image you can observe that there is an insert query to insert a new rating data in the Rating table and the batch update query to update the rating for the users.
Eager Loading:
It allows to use DbSet Method Include() to retrieve the related data in the same DB call.
public static void EargerLoadingWithRating()
{
var ratings = context.Ratings.Include(r => r.Users).ToList();
}
The Include() method in the code will retrieve the Users related data from the Ratings.

EF Core will use Left Join Query to retrieve the data, so the one query which returns back the rich objects.

var rating = context.Ratings.Include(r => r.Users).FirstOrDefault(u=>u.RatingLevel=="B");
You can also use the execution method like FirstOrDefault(), but keep in mind we should use these execution method always after the DbSet member like inlcude() as shown in below figure.

Query Projection:
If you write SQL queries then you very likely to use this, It allows to use the select() method to retrieve the related data in the same DB call.
public static void ProjectionLoadingWithRating()
{
var ratings = context.Ratings.Select(s => new { s.RatingLevel, s.Users }).ToList();
}

From the above image you can observe the result is projecting the “anonymous” type, and it only known by the method where it is defined.
EF Core can only track entities recognized by the DBContext model.
Note:
1. Anonymous type cannot be tracked
2. Entities that are the properties of an anonymous type are tracked.
Consider below code,
public static void ProjectionLoadingWithRating()
{
var ratings = context.Ratings.Select(r => new { ratings=r , B_rating= r.Users.Where(q=>q.Name.Contains("gow"))}).ToList();
var rating = ratings[1].ratings.RatingLevel += "*";
}
From the above code you can observe the local variable ratings will give a list of objects with ratings and B_rating of anonymous type and we modified the Ratinglevel from the ratings in next statement, which means Rating entity has the property RatingLevel of an anonymous type is modified.
Based on the statement “Entities that are the properties of an anonymous type are tracked” it should be tracked by the context.

From the above image it is evident that the Rating entity has the property RatingLevel of an anonymous type is modified and it is tracked by the context.
Summary:
- We have seen how to insert the related data.
- Implementing the Eager loading to fetch the related data using the DbSet member Include()
- How to use Query Projection to fetch the related data using the Select() which returns an anonymous type result.
Source Code – https://github.com/gowthamece/EFCore
Happy Coding!!!