2012-10-10 97 views
2

我傳遞值成採用foreach循環通過集合迭代的方法。在循環中,Include語句從實體框架用於急切加載。這是我通過:爲什麼這不包括工作?

var exp = new Collection<Expression<Func<Foo,object>>>(); 

爲什麼,當我使用這個:

exp.Add(f => f.Bars.Select(b=> b.Employees.Select(e=> e.Position))); 
exp.Add(f => f.Bars.Select(b=> b.Employees.Select(e=> e.Bank))); 

和員工,位置和銀行都具有場Name它將混雜之間的名稱不同的領域?如在中,銀行和頭寸都將在其名稱字段中具有員工姓名。爲了更明確,不管是什麼原因

數據庫:

Employee.Name = "Jon"; 
Employee.Bank.Name = "World Bank"; 
Employee.Position.Name = "CEO"; 

數據從.Include

Employee.Bank.Name == "Jon" //true 
Employee.Position.Name == "Jon" //true 

額外的信息,方法內接受EXP

DbSet<Foo> dbSet = context.Set<Foo>(); 
IQueryable<Foo> query = dbSet; 

if (exp != null) 
{ 
foreach (var incProp in exp) 
{ 
    query = query.Include(incProp); 
} 
} 

上午我在代碼中做錯了什麼?

編輯

public class Foo 
{ 
public int FooId { get; set; } 
public virtual List<Bar> Bars { get; set; } 
} 

public class Bar 
{ 
public int BarId { get; set; } 
public virtual Foo Foo { get; set; } 
public int FooId { get; set; } 
public virtual List<Employee> Employees { get; set; } 
} 

public class Employee 
{ 
public int EmployeeId { get; set; } 
public int BarId { get; set; } 
public virtual Bar Bar { get; set; } 
public int BankId { get; set; } 
public virtual Bank Bank { get; set; } 
public int PositionId { get; set; } 
public virtual Position Position { get; set; } 
public string Name { get; set; } 
} 

public class Bank 
{ 
public int BankId { get; set; } 
public string Name { get; set; } 
} 

public class Position 
{ 
public int PositionId { get; set; } 
public string Name { get; set; } 
} 
+0

如何'F','B'和'e'申報? SQL中的數據和代碼中的對象之間的實體框架中可能有不匹配嗎? – HeatfanJohn

+0

@HeatfanJohn - 參見編輯爲它們是如何宣佈 –

+0

@HeatfanJohn的例子 - 「在你的代碼在SQL數據和對象之間的錯配EF」,你能解釋一下你的意思是多一點嗎?顯然有一些映射錯誤,在我看來,這是SQL語句格式錯誤的結果。我不確定我的代碼是否導致聲明格式不正確。 –

回答

3

我不認爲這個問題是在你展示的代碼。我從上面的內容創建了一個控制檯應用程序,並輸出數據庫中的內容。下面是該應用程序的全部:

namespace ExampleCF 
{ 
    public class Foo 
    { 
     public int FooId { get; set; } 
     public virtual List<Bar> Bars { get; set; } 
    } 

    public class Bar 
    { 
     public int BarId { get; set; } 
     public virtual Foo Foo { get; set; } 
     public int FooId { get; set; } 
     public virtual List<Employee> Employees { get; set; } 
    } 

    public class Employee 
    { 
     public int EmployeeId { get; set; } 
     public int BarId { get; set; } 
     public virtual Bar Bar { get; set; } 
     public int BankId { get; set; } 
     public virtual Bank Bank { get; set; } 
     public int PositionId { get; set; } 
     public virtual Position Position { get; set; } 
     public string Name { get; set; } 
    } 

    public class Bank 
    { 
     public int BankId { get; set; } 
     public string Name { get; set; } 
    } 

    public class Position 
    { 
     public int PositionId { get; set; } 
     public string Name { get; set; } 
    } 

    public class Model : DbContext 
    { 
     public DbSet<Foo> Foos { get; set; } 
     public DbSet<Bar> Bars { get; set; } 
     public DbSet<Employee> Employees { get; set; } 
     public DbSet<Bank> Banks { get; set; } 
     public DbSet<Position> Positions { get; set; } 

     public Model() 
     { 
      Configuration.LazyLoadingEnabled = false; 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      Model context = new Model(); 
      var exp = new Collection<Expression<Func<Foo, object>>>(); 

      Foo foo = new Foo(); 
      Bar bar = new Bar(); 
      Employee emp = new Employee() { Name = "employee" }; 
      Bank bank = new Bank() { Name = "bank" }; 
      Position position = new Position() { Name = "position" }; 
      foo.Bars = new List<Bar>(); 
      foo.Bars.Add(bar); 
      bar.Employees = new List<Employee>(); 
      bar.Employees.Add(emp); 
      emp.Position = position; 
      emp.Bank = bank; 
      context.Foos.Add(foo); 
      context.SaveChanges(); 

      exp.Add(f => f.Bars.Select(b => b.Employees.Select(e => e.Position))); 
      exp.Add(f => f.Bars.Select(b => b.Employees.Select(e => e.Bank))); 

      DbSet<Foo> dbSet = context.Set<Foo>(); 
      IQueryable<Foo> query = dbSet; 

      if (exp != null) 
      { 
       foreach (var incProp in exp) 
       { 
        query = query.Include(incProp); 
       } 
      } 

      var first = query.ToList().FirstOrDefault(); 
      var firstEmp = first.Bars.First().Employees.First(); 
      Console.WriteLine(String.Format("{0} | {1} | {2}", firstEmp.Name, firstEmp.Bank.Name, firstEmp.Position.Name)); 
     } 
    } 

} 

輸出:employee | bank |position

還有什麼要添加到查詢,或者也許你以某種方式創建一個匿名類型?

+0

我沒有創建任何匿名類型,或者至少不是我所知道的。我在if(exp!= null)'call,'query = query.Where(foo => foo.id == 1)之前有時會過濾一次;'但是這些都是被添加的。 –

+0

我在這樣的損失。需要注意的是,數據庫是使用'' –

+0

做投影計算爲匿名類型嗎? –