c# - Filling Foreign Key Object in Entity Framework 4 -
i using entityframework first time , maybe question simple...i've used code first method..i have class personnel looks this:
public class personnel { public string id { set; get; } public int code { set; get; } public string name { set; get; } public int type { set; get; } public jobtitle title { set; get; } }
and jobtitle class:
public class jobtitle { public string id { set; get; } public int number { set; get; } public string title { set; get; } public list<personnel> personnels { set; get; } }
which last property in personnel class foreign key in personnel table of course..my problem when want retrieve personnels ( or personnel ) db using lambda expression..the foreign key object null..the lambda expression below:
context.contextinstance.personnels.tolist();
and if change expression foreign key object not null more.
context.contextinstance.personnels.include("title").tolist();
is right way??..is there better way??..i supposed ef automatically understand that!!!!..if there more 1 fk have use include of them?? please me understand.
thanks
this due lazy loading. when call context.contextinstance.personnels.tolist();
fetch personnel's title not fetch until instanced, make virtual it.
or, can disable lazy loading
public myentitiescontext() : base("name=myentitiescontext", "myentitiescontext") { this.configuration.lazyloadingenabled = false; }
doing related data context. using "include" loading on demand, when specify properties want query.
virtual keyword allows entity framework runtime create dynamic proxies entity classes , properties, , support lazy loading. without virtual, lazy loading not supported, , null on collection properties.
Comments
Post a Comment