asp.net mvc 4 - Initializing referenced objects in entity framework unit of work -
i have class in entity framework 5 (using mvc 4):
public class jobfunction { public int id { get; set; } public string jobfunctionname { get; set; } public int statusid { get; set; } public status jfstatus { get; set; } }
in onmodelcreating method, establish fk relationship status table follows:
modelbuilder.entity<jobfunction>().hasrequired(a => a.jfstatus). withmany().hasforeignkey(u => u.statusid).willcascadeondelete(false);
in controller, list of jobfunction objects follows:
list<jobfunction> jflist = uow.jobfunctionrepository.getall().tolist<catalog>();
where uow unit of work object, , jobfunctionrepository defined. when examine jobfunction object in jflist, see following in watch window:
id: 1 jfstatus: null jobfunctionname: "manager" statusid: 2
note jfstatus null. question is: provisions make in code initialize jfstatus appropriate status object (based on value of statusid), during getall call?
thanks in advance. -zawar
you need instrument apply eager loading when load data through repository. example give getall
method parameter list of expressions navigation properties want include in query:
using system.data.entity; //... public iqueryable<jobfunction> getall( params expression<func<jobfunction, object>>[] includes) { iqueryable<jobfunction> query = context.jobfunctions; foreach (var include in includes) query = query.include(include); return query; }
then call so:
list<jobfunction> jflist = uow.jobfunctionrepository .getall(jf => jf.jfstatus) .tolist();
the jfstatus
property should filled now.
Comments
Post a Comment