This is not making any sense. The query i’m running is shown below. The result of the query is data-bound to a form.
public Message GetMessage(Guid authorizationKey)
{
using (MyEntities context = new MyEntities())
{
var result = (from message in context.Messages
.Include("Attachments")
.Include("Authentication")
join authentication in context.Authentications on message.Authentication.AuthenticationId equals authentication.AuthenticationId
where authentication.AuthorizationKey == authorizationKey
select message).FirstOrDefault();
return result;
}
}
The weird shit: If i put a break-point, step through the query and data-bind everything works. Soon as i remove the breakpoint my Authentication nav property isn’t loaded. Can somebody shed some light on why this is occurring?
Include and Select do not work together.
You are telling the DataContext to return the message variable that only contains the message (even though you have the include statements on the context.Messages object). Two ways to fix this would be:
1) Project message, Attachments, Authentication into a POCO object in your select statement
2) Use the fluent notation across the whole queryable
The problem with #1 is that you will not be able to do anything with the instances of the object that would fire a query off (like a navigation), or edit any properties on those objects. Projection only gives you essentially read-only objects.
With #2, it might be better to use .SingleOrDefault instead of FirstOrDefault if your AuthorizationKey is a unique value / primary key.
The gist of the problem is that using include with select in the same IQueryable object essentially confuses the query builder expression tree and it chooses to ignore your include statements.