2010-09-23 11 views
11

Ho le seguenti classi Entity Framework POCO:Codice quadro entità prima - Eager Il caricamento non funziona come previsto?

public class Customer 
{ 
    public int Id {get;set;} 
    public string Name {get;set;} 

    public virtual ICollection<Order> Orders {get;set;} 
} 

public class Order 
{ 
    public int Id {get;set;} 
    public int CustomerId {get;set;} 
    public int OrderTypeId {get;set;} 

    public virtual OrderType Type {get;set;} 
    public virtual Customer Customer {get;set;} 
} 

public class OrderType 
{ 
    public int Id {get;set;} 
    public virtual ICollection<Order> Orders {get;set;} 
} 

Il problema è che quando torno la mia ICollection<Order> Sto ricevendo il Order s va bene, ma la proprietà del OrderOrderType non viene popolata. Il mio ordine conterrà il seguente particolare:

Id:   1 
CustomerId: 1 
Customer: Populated 
OrderTypeId: 3 
Type:  null  // Never returned 

mio codice di mappatura è simile al seguente:

public void ConfigureOrder(ModelBuilder builder) 
{ 
    // Mapping from Order -> Customer 
    builder.Entity<Order>() 
     .HasRequired(x => x.Customer) 
      .WithMany(x => x.Orders) 
       .HasConstraint((order, cust) => order.CustomerId == cust.Id); 

    // Mapping from Order -> OrderType 
    builder.Entity<Order>() 
     .HasRequired(x => x.OrderType) 
      .WithMany(x => x.Orders) 
       .HasConstraint((order, type) => order.OrderTypeId == type.Id); 
} 

Ho poi disabile pigro carico nel mio contesto:

public Context(string connectionString) 
    : base(connectionString) 
{ 
    ObjectContext.ContextOptions.LazyLoadingEnabled = false; 
} 

Quindi, per restituire i dati nel mio repository Utilizzo il metodo Include di System.Data.Entity:

var query = from item in context.Customers 
       .Include(x=> x.Orders) 
      where item.Id == customerId 
      select item; 

stavo assumendo che perché non potevo specificare Orders.OrderType, che era il problema, così ho provato alcune variazioni:

1 -> .Include(x=> x.Orders.FirstOrDefault().OrderType) 
2 -> .Include("Orders") 
3 -> .Include("Orders") 
    .Include("Orders.OrderType") 

Ma non può mai ottenere la proprietà OrderType da restituire, a meno che non appena il carico l'Ordine direttamente:

var query = from item in context.Orders 
       .Include(x=> x.OrderType) 
      select item; 

Questo codice restituirà correttamente l'OrderType nell'ordine.

risposta

22

Oh caro. Sembra che ero un asino totale. Sono le 17:45, dovrei comunque andare a casa ormai.

Avevo due metodi Get:

Get(int customerId) 
{ 
    // This was the method I was testing within 
    var query = from item in context.Customers 
        .Include("Orders.OrderType") 
       select item; 
} 

Get(int customerId, int versionId) 
{ 
    // This was the method that was running 
    var query = from item in context.Customers 
        .Include(item.Orders) 
       select item; 
} 

Quindi, "Orders.OrderType" era la corretta, anche se la soluzione alla ricerca brutto. Ho bisogno di un po 'di caffeina.

EDIT:

Solo tornando a questa domanda, il modo migliore per include è di usare System.Data.Entity 's metodo includono:

.Include(x=> x.Orders.Select(o=> o.OrderType)); 
+4

+1 per il senza corde comprendono! – ProfK

Problemi correlati