locked
C# Add record to linked table RRS feed

  • Question

  • Hi all,

    Apologies if this has been answered previously, but I couldn't find a solution that included a field with a relationship to another table.

    I have a Jobs table, and a JobHistory table which is used to store comments, notes etc. I have a block of code on the Server side that is processed when a new record is added (Jobs_Inserted). I'd like to add a note to the Job History when a new Job is added but I'm having issues with populating the fields correctly.

    partial void Jobs_Inserted(Job entity) {
    
    JobHistory SMSmessage = this.DataWorkspace.Data.JobHistories.AddNew();
               SMSmessage.Description = message;
               SMSmessage.InternalOnly = false;
               SMSmessage.Job = 1;
               this.DataWorkspace.Data.SaveChanges();
    
    }

    The issue is with SMSmessage.Job which is a linked property to the Jobs table. The error is: "Cannot implicitly convert type int to LightswitchApplication.Job.

    I had tried using:

    SMSmessage.Job = entity;

    Which I assume would be correct, but errors when the project is run in debug mode. I feel like I'm close but just missing a detail somewhere?

    Cheers,

    Adam


    Sunday, July 31, 2016 2:37 PM

Answers

  • Hi Adam, I usually put this kind of stuff in the "_Inserting" event, also I think you don't need to call SaveChanges explicitly because you are inside the Save pipeline.

    And yes, this code is correct (the navigation property is a Job entity type):

    SMSmessage.Job = entity;

    I think reading this article can be useful, let us know detailed error messages you receive, if still needed.


    Marco

    Tuesday, August 2, 2016 10:29 AM

All replies

  • Hi Adam,

    Please refer to code snippet below to modify your codes, hope it helps,

    partial void EmployeeListDeleteSelected_Execute()
            {
    
                // Write your code here.
    
                ExEmployee ex = this.DataWorkspace.ApplicationData.ExEmployees.AddNew();  // Creates a new record in ExEmployee entity.
    
                ex.FName = this.Employees.SelectedItem.FName;  // It assign the deleted Employee record to the ExEmployee properties.
    
                ex.LName = this.Employees.SelectedItem.LName;  // It assign the deleted Employee record to the ExEmployee properties.
    
                ex.Email = this.Employees.SelectedItem.Email;  // It assign the deleted Employee record to the ExEmployee properties.
    
                this.Employees.SelectedItem.Delete();  // It deletes the Employee record which is to be deleted.
    
                this.DataWorkspace.ApplicationData.SaveChanges(); // It saves the changes made to the ExEmployee.
    
            }
    
        }
    

    Regards,

    Angie


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 2, 2016 7:34 AM
  • Hi Adam, I usually put this kind of stuff in the "_Inserting" event, also I think you don't need to call SaveChanges explicitly because you are inside the Save pipeline.

    And yes, this code is correct (the navigation property is a Job entity type):

    SMSmessage.Job = entity;

    I think reading this article can be useful, let us know detailed error messages you receive, if still needed.


    Marco

    Tuesday, August 2, 2016 10:29 AM
  • Cool, thanks to you both for your help.

    It seems the issue was I was trying to call SaveChanges() while already in the save pipeline. I dropped that line, and reset SMSmessage.Job back to entity and everything is working.

    Thanks :)

    Wednesday, August 3, 2016 2:35 AM