views:

139

answers:

1

I'm updating one object, and trying to update any child objects along with it.

Basically I'm handling the OnUpdating event of a LinqDataSource.

In the DataContext class I have the ObjectUpdate function (where right now I've just got a breakpoint so I can see the values...)

In the LinqDataSource.OnUpdating event e.NewObject.Child is null, which makes no sense whatsoever. I set that to a new value, but by the time I get to DataContext.ObjectUpdate NewObject.Child has been overwritten with the OLD value...

So somewhere between LinqDataSource.Updating and DataContext.UpdateObject it's populating the object with the old values... but I need the new ones.

Is there a way to fix that, or am I going to have a nervous breakdown?

A: 

I think I figured out the problem. After running LinqDataSource through .NET Reflector I noticed that:

1) It's the LinkDataSourceUpdateEventArguments.OriginalObject which is actually attached to the data context 2) values are copied from the NewObject into OriginalObject after OriginalObject is attached to the data context

What I don't understand is why the association properties are not copied. Maybe for the same reasons you can't serialize them?

The workaround is/was to handle the Updating event myself and do the actual submit instead of letting LinqDataSource handle that part.

void FormDataSource_Updating(object sender, LinqDataSourceUpdateEventArgs e)
    {

        var newObj = e.NewObject;

        var table = FormContext.GetTable(e.NewObject.GetType());

        if (BuildingObject != null)
            BuildingObject(sender, new HeirarchicalBuildObjectEventArgs(newObj));

        table.Attach(newObj, e.OriginalObject);

        FormContext.SubmitChanges();


        e.Cancel = true;
    }
Telos