Entity Framework v5 loading relational data

I have two very simple POCO that i want to connect through a one to 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 what I have to do is very simple , but I all the resources on the internet are suprisingly so vague (or i am too dumb)

I want to write an lambda expression for

FROM Menu INNER JOIN MenuMembers 
    ON Menu.MenuId = MenuMembers.MenuId 
WHERE Menu.MenuId = 1

I have used this

IEnumerable<Menu> menu 
    = repository.Menus.Where(x => x.MenuId == menuId);

but when I iterate over it, menu.MenuNumbers stays null. I believe it is some sort of lazyloading issue.


Either Include() the relation manually for eager loading:

Entity Framework Loading Related Entities:

Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager loading is achieved by use of the Include method.

IEnumerable<Menu> menu = repository.Menus
                                   .Include(m => m.MenuMembers)
                                   .Where(x => x.MenuId == menuId);

Or mark the property as virtual so Entity Framework will lazy-load it:

Lazy loading is the process whereby an entity or collection of entities is automatically loaded from the database the first time that a property referring to the entity/entities is accessed. When using POCO entity types, lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the 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 a few other options, be sure to check out the documentation.

You need to declare MenuMembers as virtual

public virtual ICollection<MenuMember> MenuMembers { get; set; }

Need Your Help

success or error message input javascript bootstrap

javascript jquery html css twitter-bootstrap

Is there a way to change my input in order to display a succes or error icon at the input? like : http://getbootstrap.com/css/#forms-control-validation

Enforcing using the class name whenever a shared member is accessed

vb.net coding-style

We have a coding standard that says all shared (static) fields and methods must be called with the class name. E.g.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.