Sunday, 11 August 2013

Including foreign keys in lazy loading

Including foreign keys in lazy loading

So I've just changed my foreign key properties to lazy loading using the
virtual keyword.
In my entity SupportTicket I got a foreign key reference to UserProfile:
[Required]
public virtual UserProfile Owner { get; set; }
My Find method looks the following:
public static SupportTicket Find(int id)
{
using (DatabaseContext db = new DatabaseContext())
{
SupportTicket ticket = db.SupportTickets.SingleOrDefault(x => x.Id
== id);
return ticket;
}
}
My issue is the fact that whenever I get a SupportTicket from the Find
method, I cannot access the UserProfile of the SupportTicket since I'm
outside the databasecontext.
I've not used lazy loading before so am I supposed to Include() every
single foreign key in an entity?

No comments:

Post a Comment