none
Issue with related entity collection in Entity Framework 5 RRS feed

  • Question

  • I have below four Entity in my Project.

    Student

    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public int StandardId { get; set; }
    
    public virtual Standard Standard { get; set; }
    public virtual StudentAddress StudentAddress { get; set; }
    public virtual ICollection<Course> Courses { get; set; }

    Teacher

    public int TeacherId { get; set; }
    public string TeacherName { get; set; }
    public Nullable<int> StandardId { get; set; }
    
    public virtual ICollection<Course> Courses { get; set; }
    public virtual Standard Standard { get; set; }

    Standard

    public int StandardId { get; set; }
    public string StandardName { get; set; }
    public string Description { get; set; }
    
    public virtual ICollection<Student> Students { get; set; }
    public virtual ICollection<Teacher> Teachers { get; set; }

    Course

    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public string Location { get; set; }
    public int TeacherId { get; set; }
    
    public virtual Teacher Teacher { get; set; }
    public virtual ICollection<Student> Students { get; set; }

    If I try to directly access the list of courses from the student entity, I am always getting the count of courses as Zero.

       using (SchoolDBEntities db = new SchoolDBEntities())
        {
            var query = (from s in db.Students
                         select s).FirstOrDefault<Student>();
    
            foreach (var item in query.Courses)
            {
                    Console.WriteLine(item.CourseName);                    
            }
            Console.ReadLine();
        }

    But If I access the courses like below, I am getting the data properly, What is the issue in first approach? Am I missing anything? I hope the below one is not a efficient way to access the courses data.

        using (SchoolDBEntities db = new SchoolDBEntities())
        {
            var query =   (from s in db.Students                               
                             select s).FirstOrDefault<Student>();
    
            foreach (var item in query.Standard.Teachers)
            {
                foreach (var crs in item.Courses)
                {
                    Console.WriteLine(crs.CourseName);
                }
            }                
            Console.ReadLine();
        } 


    Thursday, August 29, 2013 6:12 AM

Answers

  • Hi Siva Kumar A.P,

    I download it and have a try, however, it works fine.

    There is the result below:

    And my table data is:

    Course:

    1	C001	L001	1
    2	C002	L002	1
    3	C003	L003	2
    4	C004	L004	2

    Standard:

    1	StandardName001	StandardName001
    2	StandardName002	StandardName002

    Student:

    1	Stu001	1
    2	Stu002	1
    3	Stu003	2
    4	Stu004	2

    StudentAddress:

    1	A1001	A2001	C001	S001
    2	A1002	A2002	C002	S002
    3	A1003	A2003	C003	S003
    4	A1004	A2004	C004	S004

    StudentCourse:

    1	1
    1	2
    1	3
    1	4
    2	1
    2	2
    2	3
    2	4
    3	1
    3	2
    3	3
    3	4
    4	1
    4	2
    4	3
    4	4

    Teacher:

    1	T001	1
    2	T002	2
    Regards

    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Siva Kumar A.P Wednesday, September 4, 2013 12:46 PM
    Wednesday, September 4, 2013 8:40 AM
    Moderator

All replies

  • Hello,

    Welcome toMSDN Support Forum.

    Please have a try the code below:

    var query = (from s in db.Students.Include("Courses")
                                 select s).FirstOrDefault<Student>();
                    foreach (var item in query.Courses)
                    {
                        Console.WriteLine(item.CourseName);
                    }
                    Console.ReadLine();

    More information about the include() method:

    http://msdn.microsoft.com/en-us/library/bb738708.aspx

    If this does not work for you, please let me know.

    I look forward to hearing from you.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, August 29, 2013 11:49 AM
    Moderator
  • Hi Fred,

    Thanks for your quick reply. I have tried this as well. But no luck.

    Regards,

    Siva

    Friday, August 30, 2013 6:04 AM
  • Hi,

    If it is possible, could please share your program to your skydriver.

    https://skydrive.live.com/

    Maybe I have lose some steps in my program.

    My entites are just copied from you.

    Thansk for your understanding.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, August 30, 2013 6:38 AM
    Moderator
  • Hi Fred,

    I have uploaded the code and DB Script in the below location. Please check it.

    https://skydrive.live.com/redir?resid=42728A4B738A64AF!356

    Regards, Siva


    Wednesday, September 4, 2013 6:11 AM
  • Hi Siva Kumar A.P,

    I download it and have a try, however, it works fine.

    There is the result below:

    And my table data is:

    Course:

    1	C001	L001	1
    2	C002	L002	1
    3	C003	L003	2
    4	C004	L004	2

    Standard:

    1	StandardName001	StandardName001
    2	StandardName002	StandardName002

    Student:

    1	Stu001	1
    2	Stu002	1
    3	Stu003	2
    4	Stu004	2

    StudentAddress:

    1	A1001	A2001	C001	S001
    2	A1002	A2002	C002	S002
    3	A1003	A2003	C003	S003
    4	A1004	A2004	C004	S004

    StudentCourse:

    1	1
    1	2
    1	3
    1	4
    2	1
    2	2
    2	3
    2	4
    3	1
    3	2
    3	3
    3	4
    4	1
    4	2
    4	3
    4	4

    Teacher:

    1	T001	1
    2	T002	2
    Regards

    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Siva Kumar A.P Wednesday, September 4, 2013 12:46 PM
    Wednesday, September 4, 2013 8:40 AM
    Moderator
  • Fred,

    Thanks, Its my mistake, I didn't properly add data in the Student Course Table.

    Regards, Siva 

    Wednesday, September 4, 2013 1:09 PM