Insert master and detail data with WCF RIA Service

10 posts, 0 answers
  1. Henk
    Henk avatar
    113 posts
    Member since:
    Apr 2006

    Posted 06 Oct 2010 Link to this post

    In a Silverlight client with a WCF RIA Service I want to insert Master and Detail data in one submit.
    To explain what I mean I use the Telerik OA.SL4.RIA.Demo application and model.
    In the model there is a 'Rental Order' and a 'Customer' linked to the Rental Order.
    In the Domain model on the server in the class 'Rental Order' there is a property named "Customer" with a reference to the Customer.
    On the SIlverlight client I don't see the reference properties in the generated classes. I only have the key fields available of the referenced objects.
    I want to do something like this (inserting a new Customer and a new RentalOrder in one submit):

    Customer newCustomer = new Customer();
    newCustomer.Address = "[Address]";
    newCustomer.City = "[City]";
    newCustomer.Country = "[Country]";
                      
    RentalOrder newRentalOrder = new RentalOrder();
    newRentalOrder.Customer = newCustomer;
      
    dbContext.RentalOrders.Add(newRentalOrder);
    dbContext.SubmitChanges();

    I could also do something like the code below. But that is not an option because in my own application the Key fields (in this example the CustomerID) is generated on the server. So when I need to fill the CustomerID field of the newRentalOrder the CustomerID is not available yet.

    PS: we used Oracle as backend and the PK in the master and child are generated using a sequence in a before insert trigger.

    Customer newCustomer = new Customer();
    newCustomer.CustomerID = 100;
    newCustomer.Address = "[Address]";
    newCustomer.City = "[City]";
    newCustomer.Country = "[Country]";
      
    dbContext.Customers.Add(newCustomer);
      
    RentalOrder newRentalOrder = new RentalOrder();
    newRentalOrder.CustomerID = newCustomer.CustomerID;
      
    dbContext.RentalOrders.Add(newRentalOrder);
    dbContext.SubmitChanges();

    Can you tell me why in the object classes the reference properties are not available on the Silverlight client?
    Is it not implemented yet or is it a bug. If it is not implemented yet can you tell me when this will be available?
    Is there a possible workaround?

    Kind Regards.
  2. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 12 Oct 2010 Link to this post

    Hello Henk,

     The reference properties are currently not generated out of the box. So for example if you have a relationship order->order details where one order has many orders in order to have the collection generated you will have to add some attributes to the already generated classes. For example if you have a collection of order details in your order class you will have to add the following above your property that is representing the collection:

    [Include]  
            [System.ComponentModel.DataAnnotations.Association("Order_OrderDetails_FK","OrderID","OrderID")]
            publicvirtualIList<OrderDetail> OrderDetails
            {
                get
                {
                    returnthis.orderDetails;
                }
            }

    Save and rebuild your project. This will trigger our RIA provider implementation to pick up this association and generate the proper client code.
    We are planning to extend our RIA support and include a wizard that will help you add all those attributes automatically however until then they need to be added manually.

    Best wishes,
    Petar
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. Henk
    Henk avatar
    113 posts
    Member since:
    Apr 2006

    Posted 13 Oct 2010 Link to this post

    Hello Petar,

    Thanks for your answer.
    I tried your suggestion and added the assocation to the master object.
    When I try to insert a master and a detail with the following code, In the database the ID field in the detail referencing the master in the Detailtable is Null instead of the ID of the master.
     
    Master newMaster = new Master();
    Detail newDetail = new Detail();
    newMaster.Details.Add(newDetail);
      
    dbContext.Masters.Add(newMaster);
      
    dbContext.SubmitChanges();

    I also tried to add an assocation in the detail object to the property referencing the master object.
    I removed the assocation in the master object because I received an error when I added the assocations in both.
    If I insert master and detail with the code below everything works OK.

    Master newMaster = new Master();
    Detail newDetail = new Detail();
    newDetail.Master = newMaster;
      
    dbContext.Details.Add(newDetail);
      
    dbContext.SubmitChanges();

    But I want a reference to the details in the master and a reference to the master in the detail on the client.
    I tried adding the assocations in both the master and the detail class with the extra parameter "IsForeignKey" I found.

    I added in the detail:

    [System.ComponentModel.DataAnnotations.Association("FK", "ID", "ID", IsForeignKey = true)]


    And in the master:

    [System.ComponentModel.DataAnnotations.Association("FK", "ID", "ID", IsForeignKey = false)]


    When I now try to insert master and detail using both methods explained the master record is insert once and the detail record is inserted twice.

    Do you have a solution?

    Kind Regards.
  5. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 18 Oct 2010 Link to this post

    Hello Henk,

     Can you please look into your OpenAccess configuration file and ensure that the Managed option is set to true for your details collection? Basically we have tried to achieve scenarios similar to yours and so far we did not have any problems. We will be grateful for any further information you can share with us regarding your model.

    Sincerely yours,
    Petar
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Henk
    Henk avatar
    113 posts
    Member since:
    Apr 2006

    Posted 19 Oct 2010 Link to this post

    Hi,

    Where can I find the 'Managed' option?
    Also I don't have a configuration file. I am using a Domain Model.

    Kind Regards.
  7. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 19 Oct 2010 Link to this post

    Hello Henk,

     The domain model represents your configuration file. If you open your rlinq file with an xml editor you will see the configuration. Basically the Managed attribute should be placed in the collection tag representing your collection. Here is an example:

    <orm:field name="orders" property="Orders" uniqueId="b908029d-6a9f-4623-b728-bd1de5cb5df4" type="ConsoleApplication2.Order">
               <orm:collection element-class="ConsoleApplication2.Order" Managed="true" inverse-field="customer" order-by="" uniqueId="8ca510a7-666d-43ae-adf8-a68a57fc3817" />
             </orm:field>


    Kind regards,
    Petar
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. Henk
    Henk avatar
    113 posts
    Member since:
    Apr 2006

    Posted 20 Oct 2010 Link to this post

    Hi Petar,

    I have checked the rlinq file: The Managed attribute is set to true.
    Using both methods explained the master record is inserted once and the detail record is inserted twice.
    The Identity Mechanism property of all Meta Classes is set to BackendCalculated because the key fields are filled by a database trigger. Can this be the cause of the problems?

    Kind Regards.

  9. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 25 Oct 2010 Link to this post

    Hi Henk,

     It seems there is a problem with our managed collections, however I fail to reproduce it. Everything works fine on my side. Can you perhaps provide us with a sample application that exposes the problem? We will be really grateful on your cooperation on the issue.
    We look forward to your reply.

    Kind regards,
    Petar
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  10. Henk
    Henk avatar
    113 posts
    Member since:
    Apr 2006

    Posted 27 Oct 2010 Link to this post

    Hi Petar,

    I submitted a support ticket with ticket ID 361042 where I attached an example project to reproduce the problem.

    Kind Regards.
  11. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 01 Nov 2010 Link to this post

    Hi Henk,

     Thank you for the project. I suggest we continue our conversation in the support ticket you have opened. I will post here once we find a resolution to the problem so that the rest of the community can benefit should someone encounter the same problem.

    All the best,
    Petar
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top
DevCraft banner