Before we explain, let's take a look at our database structure: Class table Student table
Greedy load
//Greedy Loadusing(vardb =Newentities ()) { varClasses = db. Classes.where (v = v.classname = ="electromechanical 10502"). Include (v =v.student); foreach(varClsinchclasses) {Console.WriteLine ("============"+CLS. ClassName); foreach(varStuinchCLS. Student) {Console.WriteLine (stu. Studentname); } }}//only one SQL statement is sent to the database using the Include method
Trace SQL statements
Description: Based on the condition query, executed a SQL, one-time loading class and all students of the class
The result of the SQL statement execution is as follows:
Traverse all classes and students:
using (var db = new entities ()) { var classes = db . Classes.include (v => v.student); foreach (var cls in classes) {Console.WriteLine ( " ============ + Cls. ClassName); foreach (var stu in CLS. Student) {Console.WriteLine (stu. Studentname); } }}
Also executes only one SQL statement, the result is as follows:
EF Learning Note 07----------load the data greedy load of the associated table