c# – 实体框架4包含表连接不起作用

我想选择装有照片和电话实体的员工.

我正在使用这样的查询:

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();

Photo和HomeTelephone属性的结果为空,但PhotoId和HomeTelephone设置为…

我做错了什么?

最佳答案 也许这解决了你的问题.

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();
点赞