1

I'm using Entity Framework for a very small database keeping track of game states in a card game. The Game has players which has cards, my models look like this:

public class Game
{
    public int Id { get; set; }
    public DateTime DateTime { get; set; }
    public virtual List<Player> Players { get; set; }
}

public class Player
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public virtual List<Card> Cards { get; set; }
}

public class Card
{
    public int Id { get; set; }
    public string Value { get; set; }
    public string Suite { get; set; }

}

It's not the best design (I know) but it gets the work done. Anyhow now I wanted to show all the games (and in effect their players and their cards) and I thought my lazy loading it would be as simple as writing:

public List<Game> GetAllGames()
{
    using (var db = new DatabaseContext())
    {
         return db.Games.ToList();
    }
}

But debugging the code, I notice that the Players attribute inside each Game object throws something called ObjectDisposedException. It seems to be that it has to do with the using statement with the db context. I searched around and found questions here on SO before regarding this and the only option seems to write LINQ to remove the virtual keyword and write LINQ instead (and I'm not sure what this should be for the things I want). So what is the purpose of lazy loading if there is no use of it outside the using statement?

3
  • 2
    To lazy load something EF should go to the database and get related entity. You disposed your DatabaseContext so connection is closed and EF cannot go to database to do that. Either use eager loading (Include) or don't dispose context while you still need it. Commented Nov 2, 2017 at 15:53
  • Yeah I understand that but then what is the point of lazy loading at all? Just to make it easier to build statements? Commented Nov 2, 2017 at 16:42
  • I personally don't like lazy loading and don't use it, but point is pretty clear: load related entities by just accessing property. There are many other scenarios besides your current one, it's not always the case that you just read something from database and immediately close connection. Commented Nov 2, 2017 at 16:46

1 Answer 1

1

You need eager loading

return db.Games.Include(x => x.Players).ToList(); 

Should do the trick

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm I tried that but the entire lambda expression is marked as an error "Cannot convert lambda expression to type "string" beacuse it is not a delegate type". Edit: Solved it by including using System.Data.Entity; But this only makes the Game include Players. How do I make the players Include the cards aswell?
@user8873750 .Include(i => i.Players.Select(c => c.Cards));? Or maybe try ThenInclude

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.