Jonas F
Jonas F

Reputation: 17

About Entity Framework, what happens at runtime

Good Night, I'm starting in AspNet Core 1.1 and I'm also using Entity FrameWork Core 1.0.

I did some courses and I already understood how to bring the data, save, etc. I want to understand what happens through "underneath the cloths" because all the tutorials I've seen explain how to use it, but I do not understand the simple "step by step" of it.

My first questions are simple, what is happening in a certain situation when we make an appointment and we have the "Context" of the BD in hand. Ex:

var viewModel = new InstructorIndexData();
    viewModel.Instructors = await _context.Instructors
          .Include(i => i.OfficeAssignment)
          .Include(i => i.CourseAssignments)
            .ThenInclude(i => i.Course)
                .ThenInclude(i => i.Enrollments)
                    .ThenInclude(i => i.Student)
          .Include(i => i.CourseAssignments)
            .ThenInclude(i => i.Course)
                .ThenInclude(i => i.Department)
          .AsNoTracking()
          .OrderBy(i => i.LastName)
          .ToListAsync();

Note: The example itself is not important, I want to understand what is happening exactly.

First doubt, when I have the Context instance "_context" what do I have at hand? I have all objects populated already loaded in the context, as this is what it looks like when I add "Includes" and "ThenIncludes" or at that moment I am just "Mounting the Query" If yes, how can I see this query being mounted?

Second question: Does the Entity Framework use LINQ? I know it's a different thing, but sometimes it feels like it's the SAME thing! You find Wheres and Orders By in both, I do not know when to use one or the other, if you can explain it to me I'll be very grateful.

Third question: It always confuses me that a query with Entity returns me, it's really VERY strange, so much so that in the examples I see people nor "typam" the return. Is there any pattern of what it returns and why does it return different things in each situation?

Thank you all !

Upvotes: 0

Views: 65

Answers (1)

BradleyDotNET
BradleyDotNET

Reputation: 61379

To answer your broadest question; Entity Framework is generating SQL, executing it against the database, and mapping the results to your data objects (a process known as Object-Relational Mapping).

To your more specific questions:

  1. _context represents your database as collections of data objects. You certainly have the metadata about all these objects (their properties and so on) but no data is loaded eagerly; so strictly speaking, your objects are not populated.

    The Include methods instruct EF to eager load those properties, so when you get an Instructor you also get his OfficeAssignments, etc without having to requery the database. Without these, accessing the navigation properties will cause a new query against the database to be performed.

  2. Yes. Entity Framework uses LINQ to Entities, which is very similar to LINQ to Objects (what you are used to using). The primary difference is that because it needs to eventually become SQL, it is more restrictive as to what operations can be performed inside any lambda expressions.

  3. I have no idea what you are talking about :)

Note: To view the generated query, follow the steps described at: https://msdn.microsoft.com/en-us/library/system.data.objects.objectquery.totracestring.aspx

Upvotes: 2

Related Questions