Entity Framework Interview Questions Tutorial
Entity Framework Interview Questions For Beginners
Q. What is Entity framework?
A. Entity Framework is an object-relation model (ORM) solution provided by Microsoft, that maps the database tables as .Net Types.
Q. How to do left join using Linq in entity framework?
A. Left join in linq is achieved by creating a GroupJoin between both the data sources or tables and then flattening out the grouping by using the SelectMany operator with DefaultIfEmpty on the grouping source to match null when the inner doesn't have a related element.
var query = from b in context.Set<Blog>()
join p in context.Set<Post>()
on b.BlogId equals p.BlogId into grouping
from p in grouping.DefaultIfEmpty()
select new { b, p };
Q. What is tracking in Entity Framework?
A. Tracking always tracks the changes in the context. It tracks and manage information of all the changes in the entities of the context. When we do save changes these changes are applied. Tracking is very useful feature of EF, but many times it is not required, for example when we need to show only details or list of entity and no need to update them, then we can make query without enabling tracking on it.
Q. How we can make a query without tracking?
A. When we query, we can use AsNoTracking extension method to diable tracking. This will optimize performance as we are not doing unnecessary tracking of entity.
var people = context.Person.AsNoTracking().ToList();