locked
Copying record and opening new Silverlight screen based on the copied record RRS feed

  • Question

  • Good evening everyone,

    I have a client that we've produced a technical costing model for. The user fills in approximately 30 fields in the Estimate entity and then code during the save method calculates around 250 other properties that are vital to the costing model. All this is good. However, due to it's complexity the customer would like a Save As button on the screen that essentially allows them to copy the record and open a new instance of it in a new Silverlight screen. I can handle passing the estimate entity and opening and closing screens fine however the copying of the entity doesn't seem to be going to plan:

    Dim newEstimate As New Estimate newEstimate = Estimate

    newEstimate.Id = 0 DataWorkspace.ApplicationData.SaveChanges() Application.ShowCreateEstimate(newEstimate.Id)

    Indeed the new record has the same Id as the one we're copying and obviously the dataworkspace rejects the save as we cannot set the id. However, if I try and set it to 0 or nothing I also get an error saying that the ID is readonly - again obviously.

    So is there a way to copy the entity WITHOUT me having to manually set all 280 odd fields? One because I'm lazy :) but two because it could be difficult to manage as the costing model with undoubtedly change over a period of time.

    Thanks.

    Paul.


    Wednesday, May 14, 2014 9:32 PM

Answers

  • I'm rather rubbish at VB these days but something along these lines works in Contoso Employee entity

            Private Sub MakeCopy_Execute()
    
                Dim e1 As Employee
                Dim e2 As Employee
                e1 = Employees.SelectedItem
                e2 = Me.Employees.AddNew()
    
                For Each p In e2.Details.Properties.All().
                    OfType(Of Microsoft.LightSwitch.Details.IEntityStorageProperty)()
    
                    If p.Name <> "Id" And p.IsReadOnly = False Then
                        p.Value = e1.Details.Properties.Item(p.Name).Value
                    End If
                Next
    
            End Sub
    

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Thursday, May 15, 2014 9:33 AM

All replies

  • I'm rather rubbish at VB these days but something along these lines works in Contoso Employee entity

            Private Sub MakeCopy_Execute()
    
                Dim e1 As Employee
                Dim e2 As Employee
                e1 = Employees.SelectedItem
                e2 = Me.Employees.AddNew()
    
                For Each p In e2.Details.Properties.All().
                    OfType(Of Microsoft.LightSwitch.Details.IEntityStorageProperty)()
    
                    If p.Name <> "Id" And p.IsReadOnly = False Then
                        p.Value = e1.Details.Properties.Item(p.Name).Value
                    End If
                Next
    
            End Sub
    

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Thursday, May 15, 2014 9:33 AM
  • You need to iterate through all the fields in the entity copying each one, except the ID. I'll see if I can dig out some code for you.

    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    Thursday, May 15, 2014 9:40 AM
  • Thanks Dave,

    Yeah, I've moved over to C# recently. This was actually the last application I wrote using VB. I come from Atari Basic > Turbo Pascal > VB3 > VB6 > VB.NET! I've been learning more C# in the past year.

    Reading up on this matter I think I get why e1 = e2 won't work... Am I right in saying that e1 = e2 just changes the pointers in memory to point at e1 rather than inheriting e1. So if I did e2.EstimateType = "Bag", e1.EstimateType would also be set to "Bag"?

    Thanks for the code snippet, I hadn't seen it before had it is certainly favourable over the some 280 odd properties I'd have to type out! It works a treat and is a lot more manageable.

    To surmise: I add a optional parameter to my create screen and check it for > 0. If it is I call that record from the db and then iterate through the entities properties as per your code. Otherwise it just creates a blank entity for new input by the user.

    'In the execute button on the details screen:

    Application.ShowCreateEstimate(Estimate.Id)

    Private Sub CreateEstimate_InitializeDataWorkspace(saveChangesTo As List(Of Microsoft.LightSwitch.IDataService))

    'Note:

    'EstimateCopy is a Local Property on the screen which is set as a parameter that is optional.

    If IsNothing(EstimateCopy) Then Me.EstimateProperty = New Estimate() Else Dim Copy As Estimate = DataWorkspace.ApplicationData.Estimates.Where(Function(e) e.Id = EstimateCopy).FirstOrDefault If Not IsNothing(Copy) Then Dim newEstimate As Estimate = New Estimate() For Each p In newEstimate.Details.Properties.All().OfType(Of Microsoft.LightSwitch.Details.IEntityStorageProperty)() If p.Name <> "Id" AndAlso p.IsReadOnly = False Then p.Value = Copy.Details.Properties.Item(p.Name).Value End If Next Me.EstimateProperty = newEstimate Else ShowMessageBox("Estimate could not be loaded. Please try again.", "Error loading Estimate", MessageBoxOption.Ok) Me.Close(False) End If End If End Sub


    Thanks for you help again Dave, I really appreciate it!

    Paul.






    Thursday, May 15, 2014 9:58 PM