asp.net mvc 4 - Virtual List is not populating in EF6 -
virtual list not populating second row of data. here model of userprofile
[table("userprofile")] public class usrprofile { [key] public int userid { get; set; } [required] public string username { get; set; } public int? introducerid { get; set; } [foreignkey("introducerid")] public virtual usrprofile introducer { get; set; } public virtual list<userinrole> userinroles { get; set; } public virtual list<usrprofile> members { get; set; } }
and userinrole model
[table("webpages_usersinroles")] public class userinrole { [key] public int roleid { get; set; } [foreignkey("roleid")] public virtual userrole userrole { get; set; } public int userid { get; set; } [foreignkey("userid")] public virtual usrprofile user { get; set; } }
here query user
public jsonresult getuser(int id) { var usr = urepository.usrprofiles.firstordefault(c => c.userid == id); var seconusrrole = usr.members[1].userinroles; //here seconusrrole null, called individually data. return json(, jsonrequestbehavior.allowget); }
is bug of or doing somthing wrong?
you not loading related entities
// using system.data.entity.core.objects; var usr = urepository.usrprofiles.include(x => x.members.select(y => y.userinroles)) .firstordefault(c => c.userid == id); var seconusrrole = usr.members[1].userinroles;
see microsoft post more information: https://msdn.microsoft.com/en-ca/data/jj574232.aspx
edit
you must add "system.data.entity.core.objects" namespace include expressions.
the include method extension methods on iqueryable<t>.
Comments
Post a Comment