Multicolumn keys in Entity Framework Code First with eager loading

I use Entity Framework, and I have a many-to-many relationship in the database between Users and Boxes, like this:

public class HistoryEntry
{
    public int BoxId { get; set; }
    public virtual Box Box { get; set; }

    public int UserId { get; set; }
    public virtual User User { get; set; }

    public int ResultBoxId { get; set; }
    public virtual Box ResultBox { get; set; }
}

The key of this "HistoryEntries" table would be a multicolumn key: it consists the BoxId and the UserId:

        modelBuilder.Entity<HistoryEntry>().HasKey(entry =>
            new
            {
                BoxId = entry.BoxId,
                UserId = entry.UserId
            });

However, I want to turn off the lazy loading and the proxy creation because I would use every query with eager loading.

How to rewrite my code in an "eager loading" style?

Answers


Use the Include method to eager load the navigational properties.

var users = context.User.Include(u => u.HistoryEntries.Select(h => h.Box))
      .Where(/* */);

Need Your Help

Service state is reported incorrectly

c#

I installed my c# application as windows service using the following command,

Distinct & Group SQL Query

php mysql distinct

I have a little bit complex MySQL query for me and i can't figure out how to write it without eating tons of memory. ( i don't really mind if it would )