实体框架4包含+表连接不起作用

时间:2011-02-17 14:29:18

标签: c# entity-framework linq-to-entities

我想选择装有照片和电话实体的员工。 我正在使用这样的查询:

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设置为......

我做错了什么?

1 个答案:

答案 0 :(得分:2)

也许这可以解决你的问题。

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