c# - Entity Framework v5 loading relational data -
i have 2 simple poco want connect through 1 many relation
public class menu { public int menuid { get; set; } public bool isactive { get; set; } public icollection<menumember> menumembers { get; set; } } public class menumember { public int menumemberid { get; set; } public int menuid { get; set; } public string viewroute { get; set; } public bool isactive{ get; set; } } public class efdbcontext : dbcontext { public dbset<page> pages { get; set; } public dbset<menu > menus { get; set; } public dbset<menumember> menumembers{ get; set; } }
now have simple , resources on internet suprisingly vague (or dumb)
i want write lambda expression for
select * menu inner join menumembers on menu.menuid = menumembers.menuid menu.menuid = 1
i have used
ienumerable<menu> menu = repository.menus.where(x => x.menuid == menuid);
but when iterate on it, menu.menunumbers
stays null. believe sort of lazyloading issue.
either include()
relation manually eager loading:
entity framework loading related entities:
eager loading process whereby query 1 type of entity loads related entities part of query. eager loading achieved use of include method.
ienumerable<menu> menu = repository.menus .include(m => m.menumembers) .where(x => x.menuid == menuid);
or mark property virtual
entity framework lazy-load it:
lazy loading process whereby entity or collection of entities automatically loaded database first time property referring entity/entities accessed. when using poco entity types, lazy loading achieved creating instances of derived proxy types , overriding virtual properties add loading hook.
public class menu { public int menuid { get; set; } public bool isactive { get; set; } public virtual icollection<menumember> menumembers { get; set; } }
and there's few other options, sure check out documentation.
Comments
Post a Comment