locked
LightSwitch 2013 - Save WCFRIA records RRS feed

  • Question

  • Hello,

    I'm building a Lightswitch 2013 HTML application.

    I have added the WCFRIA Service to my project.

    I have a screen that contains a wcfria_test query (with the following fields: ID(integer identity key), Name(string), Age(integer) ).

    The user can add records (Name and Age) and the ID is automatically calculated.

    I wanted to know if its possible to save wcfria records?

    If yes, how do i do that?

    I'v tried to save by doing: myapp.activeDataWorkspace.WCFRIA_Data.saveChanges();

    but it doesn't work.

    Am I missing something?

    Thanks.


    Friday, November 25, 2016 10:28 AM

All replies

  • Try:

    this.Context.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);

    You can find an example here:

    LightSwitch Survey: Handling Complex Business Logic Using WCF RIA Services

            public void UpdateQuestionDetailForUser(QuestionDetailForUser objQuestionDetailForUser)
            {
                // Get the current user
                string strCurrentUserName = System.Web.HttpContext.Current.User.Identity.Name;
                // We are under Forms Authentication so if user is blank then we
                // are debugging and we are TestUser
                if (strCurrentUserName == "")
                {
                    strCurrentUserName = "TestUser";
                }
                // Check for an existing Answer for this Question for this User
                var objSurveyAnswer = (from SurveyAnswers in this.Context.SurveyAnswers
                                       where SurveyAnswers.SurveyQuestion.Id == objQuestionDetailForUser.QuestionId
                                       where SurveyAnswers.UserName == strCurrentUserName
                                       select SurveyAnswers).FirstOrDefault();
                if (objSurveyAnswer != null)
                {
                    try // This is an update ****
                    {
                        // Set values
                        objSurveyAnswer.Choice = Convert.ToInt32(objQuestionDetailForUser.SelectedChoice);
                        objSurveyAnswer.Comment = objQuestionDetailForUser.Comments;
                        // Update LightSwitch Database
                        this.Context.SaveChanges(
                            System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Error inserting QuestionId " + objQuestionDetailForUser.QuestionId, ex);
                    }
                }
                else // This is an Insert ****
                {
                    // Query the GetAllQuestionsForUser method because it calculates if a Question
                    // is Active or not for the QuestionId being inserted
                    // If it is not in the collection, do not allow the insert
                    var objUserQuestion = (from QuestionForUser in this.GetAllQuestionsForUser()
                                           where QuestionForUser.UserName == strCurrentUserName
                                           where QuestionForUser.QuestionId == objQuestionDetailForUser.QuestionId
                                           select QuestionForUser).FirstOrDefault();
                    if (objUserQuestion != null)
                    {
                        try
                        {
                            // Get the Survey Question
                            var objSurveyQuestion = (from SurveyQuestions in this.Context.SurveyQuestions
                                                     where SurveyQuestions.Id == objQuestionDetailForUser.QuestionId
                                                     select SurveyQuestions).FirstOrDefault();
                            // Create a SurveyAnswer object
                            SurveyAnswer objNewSurveyAnswer = this.Context.CreateObject<SurveyAnswer>();
                            // Set values
                            objNewSurveyAnswer.UserName = strCurrentUserName;
                            objNewSurveyAnswer.Choice = Convert.ToInt32(objQuestionDetailForUser.SelectedChoice);
                            objNewSurveyAnswer.Comment = objQuestionDetailForUser.Comments;
                            objNewSurveyAnswer.SurveyQuestion = objSurveyQuestion;
                            // Update LightSwitch Database
                            this.Context.SurveyAnswers.AddObject(objNewSurveyAnswer);
                            this.Context.SaveChanges(
                                System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Error inserting QuestionId " + objQuestionDetailForUser.QuestionId, ex);
                        }
                    }
                    else
                    {
                        throw new Exception("Error inserting Answer. Answer is not marked Active.");
                    }
                }
            }


    http://AIHelpWebsite.com

    http://HoloLensHelpWebsite.com



    Friday, November 25, 2016 3:19 PM