Linq and child objects

Linq and child objects

Old forum URL: forums.lhotka.net/forums/t/4650.aspx


ajj3085 posted on Wednesday, April 09, 2008

Hi,

Using csla 3.0.4.   I'm updating my projects to start using linq instead of my custom data access layer.  Things work fine, except I seemed to have missed an important detail..

How do my child objects their their database Ids back?  My current child update code looks like this:

                if ( IsDeleted ) {
                    if ( !IsNew ) {
                        app.ApplicationId = applicationId;
                        app.Delete();
                    }

                    MarkNew();
                }
                else {
                    if ( IsNew ) {
        // The problem is here!
                        app.Insert();
                        applicationId = app.ApplicationId.Value;
                    }
                    else {
                        app.ApplicationId = applicationId;
                        app.Update();
                    }

                    MarkOld();
                }

The problem is that if I simply change the app.Insert line to db.Application.InsertOnSubmit( app ), app.ApplicationId still won't have a value until the root object calls db.SubmitChanges().

So... am I missing something obvious?

Thanks
Andy

rasupit replied on Wednesday, April 09, 2008

Andy, I ran into this when deciding to use deffer insert/update or immediate insert/update when working on the c# template for Csla 3.5.

I found the you can do both. You can hook to PropertyChanged event to update any db generated field such as identity and timestamp.
ex:
app.PropertyChanged += delegate(object o, PropertyChangedEventArgs e)
 {
     if (e.PropertyName == "ApplicationId") {
            applicationId = ((Application)o).ApplicationId;
     }
 }

Just make sure you hook the event AFTER all values are assigned.

Ricky

ajj3085 replied on Thursday, April 10, 2008

Ahh, good to know.  I wasn't sure if Linq would stuff the value into the property or into the field via reflection.  I'll give that a go!

Andy

Copyright (c) Marimer LLC