2012-10-10 2 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이 다른 분야? 에서와 같이 Bank와 Position은 둘 다 Name 필드에 Employee의 이름을가집니다. .Include에서

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

데이터 : 받아 그

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

추가 정보, 방법의 내부 특급

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)'호출 전에'filter = query.Where (foo => foo.id == 1); '라고 쓰여지기 전에 한 번 필터를 적용합니다. –

+0

나는 여기에서 그런 손실에있다. 참고로, 데이터베이스는'' –

+0

예측은 익명 유형으로 계산합니까? –

관련 문제