I'm using Entity Framework to save my data to a database. Now I have a class:
public class User
{
public int Id { get; set; }
public virtual ICollection<User> Friends { get; set; }
public virtual User BestFriend { get; set; }
public User()
{
Friends = new HashSet<User>();
}
}
I load a User with
User user;
using(var context = DatabaseContext)
{
user = context.Users.Select(x => x.Id == 1);
}
the Friends
and BestFriend
property is null
. This however is working:
User user;
using(var context = DatabaseContext)
{
user = context.Users.Include(x => x.BestFriend).Include(x => x.Friends).Select(x => x.Id == 1);
}
This leaves me with the Problem that e.g. user.BestFriend.BestFriend
is null. I could include this property, too, but this would lead only to more problem a level deeper.
How know that lazy-loading and eagerly loading does exists, but I don't know how to use any of this here to my advantage.
Thanks