locked
WCF Data Service/Entity Model - Insert Object with Child Object RRS feed

  • Question

  • Hello

    I have the following 2 database tables (master-detail)

    TestCD:
    - ID
    - Name

    TestTrack:
    - ID
    - Name
    - TestCD_ID

    Both tables are included in the entity model. A WCF Data Service accesses the Entity model.
    Now I would like to add data. I create a new TestCD-object and add multiple TestTrack-objects.

                TestCD cd = new TestCD();

                cd.ID = Guid.NewGuid();

                cd.Name = "Alice Cooper " + DateTime.Now.ToString();

     

                TestTrack track = new TestTrack();

                track.ID = Guid.NewGuid();

                track.Name = "Track 1";

                cd.TestTrack.Add(track);

     

                track = new TestTrack();

                track.ID = Guid.NewGuid();

                track.Name = "Track 2";

                cd.TestTrack.Add(track);

                track = new TestTrack();

     

                track.ID = Guid.NewGuid();

                track.Name = "Track 3";

                cd.TestTrack.Add(track);

                _ctxService.AddToTestCD(cd);

                _ctxService.SaveChanges();

    Without any WCF Data Service I can include the data easily

                _ctx.AddToTestCD(cd);

                _ctx.SaveChanges();

    But if I call the WCF data service method, only the root object (TestCD) is added. What should I do, that the child objects are added?

    Thanks for help

    Michael

     

    Tuesday, February 1, 2011 9:18 PM

Answers

  • Using the WCF Data Services client, when you are adding the related TestTrack objects, you need to also call AddRelatedObject, which both registers the new objects with the DataServiceContext and creates the relationship between the two objects. If you are using .NET Framework 3.5 SP1 instead of .NET Framework 4, you will need to instead call AddObject or AddToTestTrack and AddLink for each TestTrack.

    For more information, see Updating the Data Service (ignore the SetLink call in this topic...this is a bug in the topic as SetLink isn't really needed). 

    Cheers.

    Glenn.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Michael Giger Wednesday, February 2, 2011 8:25 AM
    Tuesday, February 1, 2011 9:26 PM

All replies

  • Using the WCF Data Services client, when you are adding the related TestTrack objects, you need to also call AddRelatedObject, which both registers the new objects with the DataServiceContext and creates the relationship between the two objects. If you are using .NET Framework 3.5 SP1 instead of .NET Framework 4, you will need to instead call AddObject or AddToTestTrack and AddLink for each TestTrack.

    For more information, see Updating the Data Service (ignore the SetLink call in this topic...this is a bug in the topic as SetLink isn't really needed). 

    Cheers.

    Glenn.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Michael Giger Wednesday, February 2, 2011 8:25 AM
    Tuesday, February 1, 2011 9:26 PM
  • Hello,

    You could display a basic example please.

    thanks
    Monday, October 21, 2013 5:48 PM
  • Here's an example from How to: Define Entity Relationships:

        // Get the specific customer. 
        var cust = (from customer in context.Customers.Expand("Orders")
                    where customer.CustomerID == customerId
                    select customer).Single();

        // Get the first order.
        Order order = cust.Orders.FirstOrDefault();

        // Create a new order detail for the specific product.
        newItem = Order_Detail.CreateOrder_Detail(
            order.OrderID, selectedProduct.ProductID, 10, 5, 0);

        // Add the new item with a link to the related order.
        context.AddRelatedObject(order, "Order_Details", newItem);

        // Add the new order detail to the collection, and 
        // set the reference to the product.
        order.Order_Details.Add(newItem);
        newItem.Order = order;
        newItem.Product = selectedProduct;

        // Send the changes to the data service.
        DataServiceResponse response = context.SaveChanges();

    Cheers,

    Glenn Gailey


    Please visit my blog

    This posting is provided "AS IS" with no warranties, and confers no rights.

    Monday, October 21, 2013 6:33 PM