none
Problem setting update,insert,delete function to work in Lightswitch VS 2012 RRS feed

  • Question

  • I was wondering if anyone could help with a problem.  I've written a test app to try out selecting, updating, deleting and inserting records with a WCF RIA service.  But I cannot get lightswitch to recognize the Update Delete or Insert functions.   The query functions work fine. 

    I've created a LS application, added a table called testtables which has two columns named ID and Name

    I then added a bunch of records into the testtables table, saved them and then refreshed to make sure they were saved. 

    I then added a new project just like the project that was added here: 

    http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/47/WCF-RIA-Service-Combining-Two-Tables.aspx

    I made sure the project was moved to .net 4.0 before adding the references specified in the above link.  I also added all of the required files.  When adding the WCF RIA Service to the Lightswitch application it sees it, adds it, and I can use it to query my testtables table, but it will not allow me to change anything on the grid that is displaying the table from my WCF RIA Service.  Any ideas on what I'm missing?

    Here's the code to the class I created:

    namespace WCFTestLib
    {
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;
        using System.Linq;
        using System.ServiceModel.DomainServices.Hosting;
        using System.ServiceModel.DomainServices.Server;
        using ApplicationData.Implementation;
        using System.Data.EntityClient;
        using System.Web.Configuration;
    
        public class TestWCF
        {
            [Key]
            public int ID { get; set; }
            public string Name { get; set; }
        }
        // TODO: Create methods containing your application logic.
        // TODO: add the EnableClientAccessAttribute to this class to expose this DomainService to clients.
        public class TestReplacement : DomainService
        {
    
            #region Database connection
            private ApplicationDataObjectContext m_context;
            public ApplicationDataObjectContext Context
            {
                get
                {
                    if (this.m_context == null)
                    {
                        string connString =
                            System.Web.Configuration.WebConfigurationManager
                            .ConnectionStrings["_IntrinsicData"].ConnectionString;
                        EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();
                        builder.Metadata =
                            "res://*/ApplicationData.csdl|res://*/ApplicationData.ssdl|res://*/ApplicationData.msl";
                        builder.Provider =
                            "System.Data.SqlClient";
                        builder.ProviderConnectionString = connString;
                        this.m_context = new ApplicationDataObjectContext(builder.ConnectionString);
                    }
                    return this.m_context;
                }
            }
            #endregion
    
            [Query(IsDefault = true)]
            public IQueryable<TestWCF> GetAllTestWCF()
            {
                var colTestWCF = from TestWCFQuery in this.Context.TestTables
                                          select new TestWCF
                                          {
                                              ID = TestWCFQuery.Id * (-1),
                                              Name = TestWCFQuery.Name,
                                          };
                return colTestWCF;
            }
            // Override the Count method in order for paging to work correctly
            protected override int Count<T>(IQueryable<T> query)
            {
                return query.Count();
            }
    
    
            public void UpdateTestWCF(TestWCF objTestWCF)
            {
                var colTestWCF = (from TestWCFQuery in this.Context.TestTables
                                  where TestWCFQuery.Id == objTestWCF.ID
                                  select TestWCFQuery).FirstOrDefault();
                if (colTestWCF != null)
                    {
                        colTestWCF.Name = objTestWCF.Name;
                        this.Context.SaveChanges();
                    }
            }
    
    
        }
    
    Friday, August 24, 2012 5:06 AM

All replies

  • Here's an update. If I override the edit button and put in some code to update the current selected item my update procedure in the WCF Service gets called.  If I don't override the button the button is greyed out. 
    Sunday, August 26, 2012 4:32 AM
  • HI

    You need to update the WCF project after you adding the code below

    public void UpdateCombinedProducts(CombinedProducts objCombinedProducts)
            {
                // If the ID is a negative number it is an Internal Product
                if (objCombinedProducts.ID < 0)
                {
                    // Internal Product ID's were changed to negative numbers
                    // change the ID back to a positive number
                    int intID = (objCombinedProducts.ID * -1);
                    // Get the Internal Product
                    var colInternalProducts = (from InternalProducts in this.Context.InternalProducts
                                               where InternalProducts.Id == intID
                                               select InternalProducts).FirstOrDefault();
                    if (colInternalProducts != null)
                    {
                        // Update the Product
                        colInternalProducts.Name = objCombinedProducts.Name;
                        colInternalProducts.Quantity = objCombinedProducts.Quantity;
                        this.Context.SaveChanges();
                    }
                }
                else
                {
                    // Get the External Product
                    var colExternalProducts = (from ExternalProducts in this.Context.ExternalProducts
                                               where ExternalProducts.Id == objCombinedProducts.ID
                                               select ExternalProducts).FirstOrDefault();
                    if (colExternalProducts != null)
                    {
                        // Update the Product
                        colExternalProducts.Name = objCombinedProducts.Name;
                        colExternalProducts.Quantity = objCombinedProducts.Quantity;
                        this.Context.SaveChanges();
                    }
                }
            }

    Tuesday, August 28, 2012 2:23 AM
    Moderator
  • I did update the project but that didn't work.  I can add the service to a new screen and I can see the add / insert and delete buttons, but when I add it to an existing screen it doesn't work.  Not sure why this is.  I is a pain to have to rebuild a screen just so I can get an RIA service inserted into it.  
    Monday, September 10, 2012 3:16 AM
  • Dear all,

    I am facing the same problem with kb57442.

    I am able to add a newly created WCF RIA to a new screen and see that all the Add, Edit & Delete buttons work.  If I were to add it to an existing screen, I get to query the WCF RIA, but any of the Add, Edit or Delete functions.

    Any ideas? 

    Best regards,

    CT




    • Edited by cte00 Friday, March 14, 2014 8:10 AM
    Friday, March 14, 2014 7:46 AM
  • Sorry for waking up an ancient thread but I just got the same issue and it took me a good half hour of headbashing before remembering the solution. Figured I would share it here since I found this thread during my desperate googeling for a solution.

    You need to tell Lightswitch data sources you want to save to and also in what order they should save changes. When you create a new screen you only have your RIA-service on your screen, in your old screen you probably also have ApplicationData.

    The solution is simple, add the data sources you want to save to in InitializeDataWorkspace and then override the Saving-method and call SaveChanges on each data source manually. In my example I first call SaveChanges on my RIA service. Set handled = true after saving so Lightswitch knows you've handled that.

            partial void MyScreen_InitializeDataWorkspace(List<IDataService> saveChangesTo)
            {
                saveChangesTo.Add(DataWorkspace.ApplicationData);
                saveChangesTo.Add(DataWorkspace.RIAServiceData);
            }

            partial void MyScreen_Saving(ref bool handled)
            {
                DataWorkspace.RIAServiceData.SaveChanges();
                DataWorkspace.ApplicationData.SaveChanges();
                handled = true;
            }



    Thursday, June 4, 2015 7:30 AM