2011-02-17 127 views
4

我想選擇加載照片和電話實體的員工。 我使用這樣的查詢:實體框架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(); 

結果有照片和HomeTelephone性空,但PHOTOID和HomeTelephone集...

我做錯了嗎?

回答

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

謝謝。我有這個想法。 – v00d00 2011-02-18 07:36:41