Entity Framework 4 Include + Table joining does not work together
I want to select the employee with loaded photo and telephone entities. I am using such query:
var empl = from user in ObjectContext.Users
from employee in ObjectContext.Employees.Include("Photo").Include("HomeTelephone")
where
user.Id == userId &&
employee.Id == user.EmployeeId &&
employee.Deleted == false &&
employee.开发者_开发知识库OwnerOrganizationId == Singleton.OrganizationId
select employee;
var result = empl.FirstOrDefault();
the result have nulls for Photo and HomeTelephone properties, but has PhotoId and HomeTelephone set...
What I am doing wrong?
Maybe this solves your problem.
User user;
using (var ctx = new Model1Container())
{
user = ctx.UserSet
.Include("Employee")
.Include("Employee.Photo")
.Include("Employee.Telefon")
.Single(x => x.Id == id);
}
Console.Out.WriteLine(user.UserName);
Console.Out.WriteLine(user.Employee.Telefon.First().Number);
Console.ReadLine();
精彩评论