none
adding existing child entity to a new parent entity then submitting

    General discussion

  • Hello,

     

    I've been pulling some serious hair out on this one. It's an issue of creating a new Parent, add a child (that already exists), then saving back to the server.

     

    I've two datagrids. The first one is a datagrid of Buses. Each bus has a sub grid (using the rowdetailstemplate) of passengers, so you can see the passengers on each bus.

     

    The second datagrid is of passengers who are not yet assigned to buses. I use a filter on the PagedCollectionView that supplies it so that it only shows passengers who have not yet assigned to a bus (where passenger.BusID = Nothing)

     

    Each Datagrid has it's own DomainDataSource, but these two share the same Domain Context.

     

    Let's say I have a pre-existing Passenger. I create a new Bus, then add this pre-existing Passenger to the bus.

    dim newBus as New Bus
    DomainDataSourceBuses.DataView.Add(newBus)
    newBus.Passengers.add(selectedPassenger)

    All is good so far, and I can change the selected bus, then go back again etc and the person stays on their bus fine.

    Now I Submit the changes back to the server.

    The correct info is sent back to the server (e.g. let's say on the server the NewBus is given ID12, then the Passenger.BusID will also be 12 on the server)

    BUT the the passenger dissapears from the rowdetailstemplate datagrid! I can't work out why. The new bus stay's there' but suddenly it doesn't have a passenger anymore, and all I've done is submit changes. When I look at the data on the server all is fine, and if i leave the application at this point then load the application again then the newbus is there with it's passenger and id works fine.

     

    The issue appears to be something to do with have an existing passenger added to a newBus, THEN submitting all that to the server in one go (so the server has to create the new bus, assign it an ID, then assign that ID to the Passenger.BusID)

     

    Any ideas what could be causing this issue? All idea would be very much appreciated, I've been pulling my hair out a while on this one!

     

    Thanks!

    Thursday, April 26, 2012 4:54 PM

All replies

  • Hello,

     

    just a quick follow-on note. I've been looking at the state of the entities (by putting preak points in the code so I can see all their details) after the submit operation has completed.

     

    If I look at the Bus - is has the correct ID assigned, but says it has no children. If I look at the passenger - it says it has a Bus (the correct one too), and the BusID property is correct. So the bus seems to forget about the passenger, but the passenger does not forget about the bus....

     

    Any ideas??

     

    thanks!

     

    Phil.

    Friday, April 27, 2012 3:35 AM
  • If I look at the Bus - is has the correct ID assigned, but says it has no children. If I look at the passenger - it says it has a Bus (the correct one too), and the BusID property is correct. So the bus seems to forget about the passenger, but the passenger does not forget about the bus....

    Hi philteare,

    It would be appreciated if you can share me more code , such as xaml and submit code.

    I suggest you to www.fiddler2.com/fiddler2 to moniter the sent data to see what's the different.

    And more , I suggest you add passenger back to the Bus to see whether it will work fine.

     

     

     

    Monday, April 30, 2012 3:58 AM
  • Hello,

     

    thanks for the reply. Here's some the bits of code.

     

    The data sources are in XAML:

     <riaControls:DomainDataSource AutoLoad="True" Name="DDSBus" QueryName="GetBusesQuery" >
                <riaControls:DomainDataSource.DomainContext>
                    <my:CDSContext />
                </riaControls:DomainDataSource.DomainContext>
            </riaControls:DomainDataSource>
    
            <riaControls:DomainDataSource AutoLoad="True" Name="DDSPassenger" QueryName="GetPassengersQuery" 
                                          DomainContext="{Binding ElementName=DDSBus, Path=DomainContext}" />

     

    And i've some buttons set to create / submit etc:

     Private Sub btnNewBus_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnNewBus.Click
            Dim newBus As New Bus
    
            newBus.Name = "Test"
            newBus.DateRequested = Today
    
            DDSBus.DataView.Add(newBus)
    
        End Sub
    
        Private Sub btnAddPassengerToBus_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnAddPassengerToBus.Click
    
            Dim currentPassenger As Person = DataGridPassenger.SelectedItem
            Dim currentBus As Bus = DataGridBus.SelectedItem
    
            currentBus.Passengers.Add(currentPassenger)
    
        End Sub
    
        Private Sub btnSave_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnSave.Click
    
            DDSBus.SubmitChanges()
    
        End Sub			

     

    Thanks!

     

    Also your post:

    And more , I suggest you add passenger back to the Bus to see whether it will work fine.

     

    I don't quite understand what you mean here?

     

    Thanks again for your post and help,

     

    Phil.

    Monday, April 30, 2012 4:40 AM
  • Hi philteare,

    According to your description ,I cannot reach the solution directly.

    I suggest you to context.Attach the object first and then add it to the Bus.

    And more, you can also try the code below:

    context.SubmitChanges( submitChangesComplited =>

    {

    //Add Passenger to Bus

    },null);

    If you still have the problem ,please let me know.

    Tuesday, May 01, 2012 11:21 PM
  • Hello,

     

    thanks for your posting. I'm still struggling with this one.

     

    I suggest you to context.Attach the object first and then add it to the Bus.

    Do you mean to add the passenger to the DomainContext before adding them to the bus? the passenger is already in the DomainContect (they already exist on the server).

     

    I've also played with your 2nd suggestion, which works well as a nice workaround.

     

    I'd still though be interested to understand what's going on here - why I can't add an pre-existing child to a New parent, then submit all at once.

     

    If it helps the parent and child each have their own DomainDataSource, but then share the same DomainContext on the client.

     

    thanks!

     

     

    Sunday, May 06, 2012 3:57 PM