2012-07-16 15 views
6

Non sono sicuro di cosa sia cambiato, ma, dopo essere tornato a un'applicazione su cui stavo lavorando alcune settimane fa, la mia chiamata .Include() non funziona più per una delle mie tabelle correlate. La parte strana è che funziona per un tavolo diverso. Ecco il codice con i commenti che mostra quello che i miei risultati sono i seguenti:Includi che non funziona con query Entity Framework

//Get the order and nothing else. 
using (OrderEntity orderContext = new OrderEntity(OrdersConnectionString)) { 
    var query = from order in orderContext.ShippingOrders 
       where order.ShipperId == shippingId 
       select order; 

    //I got a value! 
    shippingOrder = query.ToList().FirstOrDefault(); 
} 

//Get the line item and nothing else. 
using (OrderEntity orderContext = new OrderEntity(OrdersConnectionString)) { 
    var query = from orderItem in orderContext.ShippingOrderItems 
       where orderItem.ShipperId == shippingId 
       select orderItem; 

    //I got a value! 
    shippingOrderItems = query.ToList(); 
} 

Qui è dove mi sono confuso:

//Get the order *AND* the line item 
using (OrderEntity orderContext = new OrderEntity(OrdersConnectionString)) { 
    var query = from order in orderContext.ShippingOrders.Include("ShippingOrderItems") 
       where order.ShipperId == shippingId 
       select order; 

    //I get a ShippingOrder result, but no items are returned. I used the SQL Server Profiler and saw the SQL that got executed; it contains the item, EF just isn't loading the object. 
    shippingOrder = query.ToList().FirstOrDefault(); 
} 

sono in grado di tornare risultati per una tabella correlata diverso. Questo mi fa pensare che l'EF manchi la relazione tra il mio ordine e la tabella degli elementi pubblicitari, ma non sono sicuro di come possa risolverlo.

Edit: Ecco l'Ordine Entity

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
[EdmEntityTypeAttribute(NamespaceName="OrderModel", Name="ShippingOrder")] 
[Serializable()] 
[DataContractAttribute(IsReference=true)] 
public partial class ShippingOrder : EntityObject 
{ 
    #region Factory Method 

    /// <summary> 
    /// Create a new ShippingOrder object. 
    /// </summary> 
    /// <param name="shipperId">Initial value of the ShipperId property.</param> 
    public static ShippingOrder CreateShippingOrder(global::System.String shipperId) 
    { 
     ShippingOrder shippingOrder = new ShippingOrder(); 
     shippingOrder.ShipperId = shipperId; 
     return shippingOrder; 
    } 

    #endregion 
    #region Primitive Properties 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.String ShipperId 
    { 
     get 
     { 
      return _ShipperId; 
     } 
     set 
     { 
      if (_ShipperId != value) 
      { 
       OnShipperIdChanging(value); 
       ReportPropertyChanging("ShipperId"); 
       _ShipperId = StructuralObject.SetValidValue(value, false); 
       ReportPropertyChanged("ShipperId"); 
       OnShipperIdChanged(); 
      } 
     } 
    } 
    private global::System.String _ShipperId; 
    partial void OnShipperIdChanging(global::System.String value); 
    partial void OnShipperIdChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
    [DataMemberAttribute()] 
    public global::System.String OrderNumber 
    { 
     get 
     { 
      return _OrderNumber; 
     } 
     set 
     { 
      OnOrderNumberChanging(value); 
      ReportPropertyChanging("OrderNumber"); 
      _OrderNumber = StructuralObject.SetValidValue(value, true); 
      ReportPropertyChanged("OrderNumber"); 
      OnOrderNumberChanged(); 
     } 
    } 
    private global::System.String _OrderNumber; 
    partial void OnOrderNumberChanging(global::System.String value); 
    partial void OnOrderNumberChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
    [DataMemberAttribute()] 
    public Nullable<global::System.DateTime> OrderDate 
    { 
     get 
     { 
      return _OrderDate; 
     } 
     set 
     { 
      OnOrderDateChanging(value); 
      ReportPropertyChanging("OrderDate"); 
      _OrderDate = StructuralObject.SetValidValue(value); 
      ReportPropertyChanged("OrderDate"); 
      OnOrderDateChanged(); 
     } 
    } 
    private Nullable<global::System.DateTime> _OrderDate; 
    partial void OnOrderDateChanging(Nullable<global::System.DateTime> value); 
    partial void OnOrderDateChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
    [DataMemberAttribute()] 
    public global::System.String CustomsComment 
    { 
     get 
     { 
      return _CustomsComment; 
     } 
     set 
     { 
      OnCustomsCommentChanging(value); 
      ReportPropertyChanging("CustomsComment"); 
      _CustomsComment = StructuralObject.SetValidValue(value, true); 
      ReportPropertyChanged("CustomsComment"); 
      OnCustomsCommentChanged(); 
     } 
    } 
    private global::System.String _CustomsComment; 
    partial void OnCustomsCommentChanging(global::System.String value); 
    partial void OnCustomsCommentChanged(); 

    #endregion 

    #region Navigation Properties 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [XmlIgnoreAttribute()] 
    [SoapIgnoreAttribute()] 
    [DataMemberAttribute()] 
    [EdmRelationshipNavigationPropertyAttribute("OrderModel", "FK_ShippingOrderItem_ShippingOrder", "ShippingOrderItem")] 
    public EntityCollection<ShippingOrderItem> ShippingOrderItems 
    { 
     get 
     { 
      return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<ShippingOrderItem>("OrderModel.FK_ShippingOrderItem_ShippingOrder", "ShippingOrderItem"); 
     } 
     set 
     { 
      if ((value != null)) 
      { 
       ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<ShippingOrderItem>("OrderModel.FK_ShippingOrderItem_ShippingOrder", "ShippingOrderItem", value); 
      } 
     } 
    } 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [XmlIgnoreAttribute()] 
    [SoapIgnoreAttribute()] 
    [DataMemberAttribute()] 
    [EdmRelationshipNavigationPropertyAttribute("OrderModel", "FK_ShippingOrderItemTracking_ShippingOrder", "ShippingOrderTracking")] 
    public EntityCollection<ShippingOrderTracking> ShippingOrderTrackings 
    { 
     get 
     { 
      return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<ShippingOrderTracking>("OrderModel.FK_ShippingOrderItemTracking_ShippingOrder", "ShippingOrderTracking"); 
     } 
     set 
     { 
      if ((value != null)) 
      { 
       ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<ShippingOrderTracking>("OrderModel.FK_ShippingOrderItemTracking_ShippingOrder", "ShippingOrderTracking", value); 
      } 
     } 
    } 

    #endregion 
} 
+0

Scatto lungo ... hai un costruttore vuoto sulla classe di ShippingOrderItem? – Phil

+0

Puoi pubblicare l'entità ShippingOrder? Qual è il nome della proprietà (e della configurazione) di ShippingOrderItem? –

+0

@PhilCartmell - Solo una FYI: sto usando il codice generato automaticamente (prima il database). Ho esaminato la classe e non è stato specificato alcun costruttore, quindi, C# magic mi sta lasciando con un costruttore vuoto. –

risposta

1

io non sono ancora sicuro di quale sia la causa del problema è stato. Ho deciso di abbandonare il mio database e ricreare le mie tabelle (insieme alle chiavi primarie e esterne) e migrare nuovamente su tutti i dati. In realtà è stato corretto, ma non sono sicuro di cosa sia finito per essere diverso. Non stavo registrando eccezioni e, in base a SQL Server Profiler, sembrava che fosse in esecuzione la query corretta.

+0

Qualche ragione per cui non hai accettato questa risposta? –

+0

@ShadowWizard - Volevo tornare e accettare questa risposta come risposta.Grazie .. –

+0

Lol .. Beh, meglio tardi che mai! :) –

1

Si può anche provare questo:

if (Order.shippingId != null && Order.shippingId > 0) 
    orderContext.LoadProperty(Orders, Order => Order.ShippingOrderItems); 

Questo sarà recuperare l'id ordine corrispondente in ShippingOrderItems.

Problemi correlati