2012-07-16 12 views
6

No estoy seguro de qué cambió, pero, después de volver a una aplicación en la que estaba trabajando hace unas semanas, mi llamada .Include() ya no funciona para una de mis tablas relacionadas. La parte extraña es que funciona para una mesa diferente. Aquí hay un código con comentarios mostrando lo que mis resultados son los siguientes:Incluir no funciona con la consulta de 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(); 
} 

Aquí es donde estoy confundido:

//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(); 
} 

soy capaz de volver resultados de una tabla relacionada diferente. Esto me hace pensar que a EF le falta la relación entre mi pedido y la tabla de partidas, pero no estoy seguro de cómo puedo solucionarlo.

Editar: Aquí está la Orden Entidad

/// <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

muy remota ... ¿tiene un constructor vacío en su ShippingOrderItem (s) clase? – Phil

+0

¿Puedes publicar la entidad ShippingOrder? ¿Cuál es el nombre de la propiedad (y configuración) del elemento ShippingOrderItem? –

+0

@PhilCartmell - Solo un FYI: estoy usando el código generado automáticamente (primero en la base de datos). Miré la clase y no hay un constructor especificado, entonces, la magia de C# me deja con un constructor vacío. –

Respuesta

1

Todavía no estoy seguro de lo que fue la causa del problema. Decidí simplemente dejar caer mi base de datos y volver a crear mis tablas (junto con las claves primaria y externa) y migrar de nuevo todos los datos. Realmente lo solucionó, pero no estoy seguro de qué terminó siendo diferente. No recibía ninguna excepción registrada y, en base al Analizador de SQL Server, parecía que se estaba ejecutando la consulta correcta.

+0

algún motivo usted no aceptó esta respuesta –

+0

@ShadowWizard - me refería a volver y aceptar esto como una respuesta Gracias –

+0

Lol .... ¡Bueno, mejor tarde que nunca! :) –

1

También puede probar esto:

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

Esto obtendrá el número de pedido coincidente en ShippingOrderItems.

Cuestiones relacionadas