views:

7

answers:

1

Hello everybody :-)

I got a question about inserting an entity with dependent entities using RIA Services (With Silverlight 4.0).

Let's say I have an Entity in my (sql) database called "Beer" and one called "Supplier", with a relationship: Beer 1 - n Supplier. There are multiple suppliers for one kind of beer.

Now there's the following use case: The user enters a new beer with, let's say, 5 suppliers.

On the silverlight view I now got two DomainDataSource's. On the Beer DomainDataSource I add and submit the new beer and on the Supplier DomainDataSource I submit the now suppliers, which contain a foreign key which links them to the beer.

My question is: How can I make sure that the Beer gets submitted first and afterwards the dependent (remember the foreign key) Suppliers?

I am aware that I could simply chain up the SubmitChanges() using the OnSubmitted event. But this solution is pretty... well... lame. It makes for some really ugly code.

Thanks for all your numerous ideas!

A: 

Unfortunately there is no way to force the order of the updates that come in the same ChangeSet.

However, if all new suppliers are submitted to the server with new beers (a big IF), you could manually check the ChangeSet in your Upddate method:

public void UpdateBeer(Beer beer)
{
    foreach(ChangeSetEntry changeSetEntry in ChangeSet.Entries)
    {
        if (changeSetEntry.Entity.GetType() == typeof(Supplier))
        {
            Supplier supplier = (Supplier)changeSetEntry.Entity;
            UpdateSupplierInternal(supplier);
        }
    }

    DataContext.Beers.Attach(beer, ChangeSet.GetOriginal(beer));
}

That calls a separate method to update supplier. You still need an UpdateSupplier method or RIA will throw an exception when it exists in the ChangeSet, but the method should do nothing:

public void UpdateSupplier(Supplier supplier)
{
    // do nothing
}
krolley