none
Master - Detail Question? Customer Order / Order Details / Product ... RRS feed

  • Question

  • Hi,

    I've a winforms entity framework 4 project that I'm struggling with.  I'm new to using Entity Framework so hopefully its an easy one.

    My example is complex so for explanation purposes I'm going to to use an example of an Customer Order, Order Details and Products example.

    In this example I have an Order list. A user clicks New Order. We create a new instance of an order "ctx.Orders.CreateObject()" and pass the new order object and a reference to the context to a form that shows a new order. On the order form we set the a data source control to the new order object while having all the fields on the order form bound using that data source.

    One of the controls on the form is a grid of order details which is bound to the order.orderdetails. Similarly, to add an order details line the user clicks on the add button which creates an order details object and passes it to an order details form which shows a list of products and lets them specify some details around the order detail record. The user can then closes this form which adds the newly created orderdetails object to the order.orderdetails list. The user then clicks the SAVE order which saves the entire order and details in one go. This works with one small problem that I cannot figure out! So the problem .... our orderdetails grid contains a reference to Products.Description. When an order detail is added to the order.orderdetails it appears on the grid as an additional line, but any part of the orderdetails that refers to another entity is not available. ie. OrderDetails.ProductID is available but OrderDetails.Product.Name is not.  I've tried AttachTo which seemed to work but created issues when we tried to save the record.  Any suggestions how to add an order detail line to the orderdetails list / datasource ensuring that it is members are navigable?

    Monday, July 2, 2012 3:02 PM

Answers

  • Hi Aregen,

    If your case is simple then, you could able to check using is IsLoaded to navigation entity. e.g.

    boolean checkIsLoaded = yourContext.Entry(OrderDetails).Reference(od => od.Product).IsLoaded;

    you can force to load the navigation entity by using Load() after SaveChange() has been called. e.g.

    yourContext.Entry(OrderDetails).Reference(od => od.Product).Load();

    Hope this helps.


    Tuesday, July 3, 2012 10:14 AM