Enable not work with Entity Framework request

I'm not sure what has changed, but returning to the application I was working on a few weeks ago, my .Include() call no longer works for one of my linked tables. The strange part is that it works for another table. Here are a few comment codes showing my results:

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

This is where I am confused:

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

I can return results for another related table. This makes me think that in EF there is no connection between my order and the position table, but I'm not sure how to fix it.

Change Here is the Order object

 /// <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 } 
+6
source share
2 answers

I'm still not sure what the cause of the problem is. I decided to simply drop my database and recreate my tables (along with the primary and foreign keys) and migrate all the data again. This has actually been fixed, but I'm not sure that everything has changed in the end. I did not get any exceptions registered on the basis of SQL Server Profiler, and looked like the correct query was being executed.

+1
source

You can also try the following:

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

This will display the matching order ID in ShippingOrderItems.

+1
source

Source: https://habr.com/ru/post/920596/


All Articles