locked
LightSwitch RIA Services INSERT method RRS feed

  • Question

  • I have been experimenting with WCF RIA services in my application using the great stuff from Michael Washington here.  I can select and update query through the RIA service no problem, but I can't figure out how to insert a new record.  Can someone please share some sample code for doing an insert with RIA Services?  Thanks.

    Tuesday, October 29, 2013 1:42 PM

Answers

  • I just want to add an additional response because the exact syntax to use when saving to the LightSwitch data context may not be apparent.

    1) Put the word "Insert" in the method

    2) Have the method take, as a parameter, the class that you want to save

    3) Use syntax like this:

            public void InsertAuthorOffers(Offer objOffer)
            {
                // Create the Offer
                var objSaveOffer = this.Context.CreateObject<Offer>();
                objSaveOffer.Active = objOffer.IsActive;
                objSaveOffer.Description = objOffer.Description;
                objSaveOffer.IsPrivate = objOffer.IsPrivate;
                objSaveOffer.OfferAmount = objOffer.OfferAmount;
                objSaveOffer.LastIPAddress = GetIPAddress();
                objSaveOffer.LastUpdate = DateTime.Now;
    
                // Update LightSwitch Database
                this.Context.Offers.AddObject(objSaveOffer);
                this.Context.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
            }


    Unleash the Power - Get the LightSwitch HTML Client book

    http://LightSwitchHelpWebsite.com



    • Edited by ADefwebserver Wednesday, October 30, 2013 12:36 PM
    • Marked as answer by Hessc Wednesday, October 30, 2013 12:37 PM
    Wednesday, October 30, 2013 12:30 PM

All replies

  • MSDN Documentation: Domain Services. Please see the "Insert" section on how to define the method on your domain service.


    Justin Anderson, LightSwitch Development Team

    Wednesday, October 30, 2013 8:21 AM
    Moderator
  • I just want to add an additional response because the exact syntax to use when saving to the LightSwitch data context may not be apparent.

    1) Put the word "Insert" in the method

    2) Have the method take, as a parameter, the class that you want to save

    3) Use syntax like this:

            public void InsertAuthorOffers(Offer objOffer)
            {
                // Create the Offer
                var objSaveOffer = this.Context.CreateObject<Offer>();
                objSaveOffer.Active = objOffer.IsActive;
                objSaveOffer.Description = objOffer.Description;
                objSaveOffer.IsPrivate = objOffer.IsPrivate;
                objSaveOffer.OfferAmount = objOffer.OfferAmount;
                objSaveOffer.LastIPAddress = GetIPAddress();
                objSaveOffer.LastUpdate = DateTime.Now;
    
                // Update LightSwitch Database
                this.Context.Offers.AddObject(objSaveOffer);
                this.Context.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
            }


    Unleash the Power - Get the LightSwitch HTML Client book

    http://LightSwitchHelpWebsite.com



    • Edited by ADefwebserver Wednesday, October 30, 2013 12:36 PM
    • Marked as answer by Hessc Wednesday, October 30, 2013 12:37 PM
    Wednesday, October 30, 2013 12:30 PM
  • Thanks Justin and Michael.  It was CreateObject() that I was missing.  I was trying to use AddObject() to create the object and it was giving me fits.  Now I see why.  Thanks!
    Wednesday, October 30, 2013 12:40 PM