.net - Issue with related entity collection in Entity Framework 5 -
i have below 4 entity in 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 try directly access list of courses student entity, getting count of courses 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 access courses list below, getting data properly, issue in first approach? missing anything? hope below 1 not efficient way access 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(); }
Comments
Post a Comment