none
Entity Framework Not include child Object RRS feed

  • Question

  • Class Table

    ID (Identity)

    Name

    Description

    1

    Name1

    TestData

    2

    Name2

    TestData2


    StudentClass Table

    ID

    ClassID(Reference Class table)ID Column

    StudentID

    Description

    1

    1

    1

    Test1

    2

    1

    2

    Test2

    3

    2

    3

    TestData123

    When I  execute sql Query :

    select * from class as c  inner join studentclass sc on c.ID=sc.ClassID where StudentID=1

    result getting as expected 

    Now I have access through Entity Framework.

    I have created one class called “Class” below property

             public int ID { get; set; }

            public string Name { get; set; }

            public string Description { get; set; }

             public virtual ICollection<StudentClass> StudentClass { get; set; }

    EntityFramework

      var result = (from c in dbContext.Class  join sc in dbContext.StudentClass on c.ID equals sc.ClassID

                                  where sc.StudentID == 1  select c).Include(p => p.StudentClass)

                                  .ToList();

    In above query when I see studentclass object have 2 row it not filter by studentID.

    can some one help me why child entity not include properly

    Thanks


    • Edited by Mayank777687 Thursday, August 11, 2016 11:53 AM
    • Moved by CoolDadTx Thursday, August 11, 2016 3:34 PM EF related
    Thursday, August 11, 2016 4:11 AM

Answers

  • Hi,

    as your are only selecting dbContext.Class you will only get those objects. The Include Extensions only assures that the StudentClass are loaded. To filter the loaded StudentClass entities apply the where clause inside the Include and turn off lazy loading, see Loading Related Entities.

    But that not the same as SELECT *: that's a SQL short notation for SELECT c.*, sc.*. If you want all values in one class in C# you will have to create a new object, for example select new { c, sc }. That will result in a anonymous class object containing both classes.

    The better solution is to access the StudentClass using the collection property of the the Class instance, as it avoids additional memory consumption.

    Regards, Elmar

    Thursday, August 11, 2016 8:08 AM

All replies

  • Hi,

    as your are only selecting dbContext.Class you will only get those objects. The Include Extensions only assures that the StudentClass are loaded. To filter the loaded StudentClass entities apply the where clause inside the Include and turn off lazy loading, see Loading Related Entities.

    But that not the same as SELECT *: that's a SQL short notation for SELECT c.*, sc.*. If you want all values in one class in C# you will have to create a new object, for example select new { c, sc }. That will result in a anonymous class object containing both classes.

    The better solution is to access the StudentClass using the collection property of the the Class instance, as it avoids additional memory consumption.

    Regards, Elmar

    Thursday, August 11, 2016 8:08 AM
  • Hi Mayank777687,

    Based on your description and code snippet, it seems that you retrieve a Class  object which include two studentclass object.

    If you want to retrieve related studentclass, you could modify your code like this:

    var result = from c in dbContext.Class  join sc in dbContext.StudentClass on c.ID equals sc.ClassID
    
                                  where sc.StudentID == 1  select new 
    {
        id = c.ID,
        stuid = sc.ID
    
    // other field....
    }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, August 31, 2016 6:04 AM
    Moderator