2012-07-16 77 views
6

我不确定发生了什么变化,但在回到几周前正在处理的应用程序后,我的.Include()调用不再适用于我的相关表之一。奇怪的部分是它适用于不同的表格。下面是一些代码注释显示什么我的结果是:包括不与实体框架查询一起工作

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

这里是我很困惑:

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

我能够找回结果不同的相关表。这让我认为EF缺少我的订单和订单项表之间的关系,但是,我不确定我如何解决该问题。

编辑:这里是Order实体

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

远射...你的ShippingOrderItem类有空的构造函数吗? – Phil 2012-07-16 14:23:47

+0

你能发布ShippingOrder实体吗? ShippingOrderItem的属性(和配置)的名称是什么? – 2012-07-16 14:24:38

+0

@PhilCartmell - 仅供参考:我正在使用自动生成的代码(数据库优先)。我看了一下这个类,并没有指定构造函数,所以,C#魔法让我留下了一个空的构造函数。 – 2012-07-16 14:27:57

回答

1

我仍然不知道问题的原因了。我决定放弃我的数据库并重新创建我的表(连同主键和外键)并再次迁移所有数据。它实际上修复了它,但是,我不确定最终会有什么不同。我没有收到任何异常记录,并且基于SQL Server Profiler,它看起来像正在执行正确的查询。

+0

任何你不接受这个答案的原因? – 2012-08-27 06:51:44

+0

@ShadowWizard - 我的意思是回来接受这个答案。谢谢。 – 2012-08-28 13:38:49

+0

Lol ..好吧,迟到比从未更好! :) – 2012-08-28 13:45:12

1

你也可以试试这个:

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

这将在ShippingOrderItems获取匹配订单ID。