Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Exeption when rehydrating the orderlines RSS

2 replies

Last post Jul 08, 2009 08:57 AM by theo67

(0)
  • theo67

    theo67

    Member

    532 Points

    181 Posts

    Exeption when rehydrating the orderlines

    Jul 07, 2009 10:59 AM | LINK

    I have this order/orderline association:

     

        public class OrderEntity
        {
            public OrderEntity()
            {
            }
    ...
            [Association( "Order_Orderlines", "ClientId,OrderId", "ClientId,OrderId,OrderlineId" )]
            [Include]
            public List<Orderline> Orderlines
            {
                get;
                set;
            }
        }
    
        public class OrderlineEntity
        {
    ...
        }
     

    When I change the order/orderlines on the client by code:

     

                private void UpdateFirstOrder(IEnumerable orders)
                {
                    OrderEntity firstOrder = orders.First();
                    if (firstOrder == null)
                        return;
    
                    firstOrder.OrderDate = DateTime.Now;
                    foreach (OrderlineEntity orderline in firstOrder.Orderlines)
                    {
                        orderline.ProductId = 12345;
                    }
                }
    
     

    and submit the changes via the domain context:

     

                    OrderEntryDomainContext _service...
    ...
                    SubmitOperation operation = _service.SubmitChanges();
                    operation.Completed += new EventHandler( OnSubmitCompleted );
    
     

    I get the following exception:

    One or more associated objects were passed for collection property 'Orderlines' on type 'OrderEntity', but the target collection is null.

       at System.Web.Ria.DataServiceSubmitRequest.SetAssociationMember(Object entity, PropertyDescriptor associationProperty, IEnumerable`1 associatedEntities)
       at System.Web.Ria.DataServiceSubmitRequest.SetEntityAssociations(List`1 entityOperations)
       at System.Web.Ria.DataServiceSubmitRequest.GetChangeSet(DomainService domainService)
       at System.Web.Ria.DataServiceSubmitRequest.Invoke(DomainService domainService)
       at System.Web.Ria.DataService.System.Web.IHttpHandler.ProcessRequest(HttpContext context)

    Now this can be fixed by changing the constructor of the Order entity to:

     

        public class OrderEntity
        {
            public OrderEntity()
            {
                Orderlines = new List<Orderline>(); // add initializer
            }
    

     

    Why is this necessary? Is this a bug in the deserialization logic?

    Theo.
  • WilcoB

    WilcoB

    Participant

    786 Points

    146 Posts

    Re: Exeption when rehydrating the orderlines

    Jul 07, 2009 10:43 PM | LINK

    In this specific case it's not necessary. We could instantiate the list for you. However, if you're using an abstract type we wouldn't always be able to instantiate it for you. Or if there's no setter. We decided to keep things simple for now and throw instead of trying to figure out how to instantiate the list for you.

    This will very likely change in the future though as we revisit our protocol layer.

    - Wilco Bauwer (MSFT) / http://www.wilcob.com
  • theo67

    theo67

    Member

    532 Points

    181 Posts

    Re: Exeption when rehydrating the orderlines

    Jul 08, 2009 08:57 AM | LINK

    Thanks! The misleading part was the exception message, since it was not obvious I had to add the initializer. Are there any guidelines about how to declare your entity? Like you say I actually don't want the Orderlines-property to be set externally, it just needs to be a valid collection. The same issue I have with the associations: I have to include the foreign keys in my entity else I can't specify the Association-attribute. So in the case of my Order-entity I expose a CustomerId as well as the Customer entity:

            [Required()]
            [StringLength( 10 )]
            public string CustomerId // Required for now, since we need it to define the association in Customer
            {
                get;
                set;
            }
    
    
            //[Required()]
            [Association( "Order_Customer", "ClientId,CustomerId", "ClientId,CustomerId", IsForeignKey = true )]
            [Include]
            public CustomerEntity Customer
            {
                get;
                set;
            }
    

    I don't think there is an alternative, is there? 

     

    Theo.