Load child entity on the fetch of the Parent entity EFCore

前端 未结 3 1025
感情败类
感情败类 2021-01-22 16:50

I have the below model. What is the better way to load the parent entity with child entity at the time of fetching from the DB with find method?

Parent Entity:

3条回答
  •  再見小時候
    2021-01-22 17:25

    You can use Include()

    Linq query

    using (var context = new DBEntities())
    {
       var result = (from c in context.Client.Include("Address")
                where c.IsActive
                select c).ToList();
    }
    

    Lambda Expression

    using (var context = new DBEntities())
    {
       var result = context.Client.Include(p => p.Address).Where(c => c.IsActive).ToList();
    }
    

提交回复
热议问题