0

I'm using EF Core 2.2 and have following schema:

public class Person
{
    public int Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public Passport Passport { get; set; }
}

public class Passport
{
    public int Id { get; set; }

    public string Number { get; set; }

    public DateTime IssueDate { get; set; }

    public string Authority { get; set; }

    public int PersonId { get; set; }
}


When i'm doing following request:

var persons = _context.Persons
    .Include(p => p.Passport)
    .Select(p => new
    {
        PersonId = p.Id,
        FirstName = p.FirstName,
        LastName = p.LastName,
        PassportNumber = p.Passport.Number
    }).ToList();

EF Core translates it into SQL which contains all the fields of Passport entity. While it's ok for small schema but it can be gap for complicated related entities. Is that possible to make EF Core to send request to db selecting only fields mentioned in LINQ Select statement?

Thank you in advance

ProShav
  • 13
  • 2
  • Did you check out https://stackoverflow.com/a/49597502/2490286? – misha130 May 29 '20 at 19:40
  • 1
    I'm surprised it doesn't ignore the `Include`, AFAIK it should (EF core 2.2 shouldn't be different). Anyway, why do you have the `Include` there in the first place? You can remove it. BTW, do you know that EF core 2.2 isn't supported any more? – Gert Arnold May 29 '20 at 20:55
  • @GertArnold If you omit Includes you end up with Lazy Loading, as i check in profiler EF just sends SELECT Request without join, it happens after when you access it – ProShav May 30 '20 at 11:10
  • 1
    Then you're not talking about the query you show, which shouldn't `Include` nor lazy load. Unless `_context` isn't an EF context and `_context.Persons` isn't an EF `DbSet`. – Gert Arnold May 30 '20 at 12:54

0 Answers0