Cannot insert child objects (Possible stale data in FieldMAnager)??

Cannot insert child objects (Possible stale data in FieldMAnager)??

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


ryanlio posted on Friday, February 19, 2010

Hi, appreciate any help on this.

I've got a parent object A, and child objects B.

parent A consists of a a field A1 that only gets updated after parent object A gets inserted into the database (similar to an identity field)

Now, after the parent get inserted and field A1 gets the returned value, the new value of A1 get updated on the child objects' field B1.

The above are achieved by having child list B subscribed to an event of parent A.

However, when the child objects are inserted into the database via FieldManager.UpdateChildren(this). Somehow the child field B1 still contains an empty value even though the iteration to update the objects are looped thru.

 

Codes:

Child's DataPotal_Insert()

protected override void DataPortal_Insert()

{

using(SafeDataReader reader = DataAccessLayer.Instance.MSSKitSetInsert(ReadProperty(_mSSNoProperty), ReadProperty(_remarksProperty)))

{

if(reader.Read())

{

                    SetProperty(_mSSNoProperty, reader.GetString("NewMssNo"));

}

}

            //Raise an Event to tell child objects to update with New MssNo

            OnMSSKitSetSaved(this, new Csla.Core.SavedEventArgs(this));

           //!!!! SOMEHOW FIELDMANAGER STILL HOLDS THE PREVIOUS CHILD OBJECT B, FIELD B1's VALUE!!!!

            FieldManager.UpdateChildren(this);

}

 

List of Child Objects B

//Code to run when parent object is Saved

        void MSSKitSet_MSSKitSetSaved(object sender, Csla.Core.SavedEventArgs e)

        {

            MSSKitSet mssKitSet = (MSSKitSet)sender;

            foreach (MSSKitSetItem item in this)

            {

                item.MSSNo = mssKitSet.MSSNo;

            }

        }

Ryan

triplea replied on Friday, February 19, 2010

Is it possible that you simply are not subscribing to the parent event correctly? Maybe you could provide some codeto display how you do that or try setting a breakpoint in the handler to see if you ever actually update the value after OnMSSKitSetSaved(this, new Csla.Core.SavedEventArgs(this)); is called in the first place.

Also, if _mSSNoProperty only gets updated once on insert, would it not be simpler to just loop through your child collection and update the value(s) manually?

Marjon1 replied on Friday, February 19, 2010

I'd agree with triplea that it would be easier to just have a method on ObjectB were you could set the appropriate value, if this applied to all the items within the collection or only one it doesn't really matter, the method would determine the inner workings and could pass in the new value at the same time.

ryanlio replied on Friday, February 19, 2010

Hi Triplea, 

We make changes to your recommendations, and it working fine now.

Its weird that the our previous implementation has got the event triggered and properties updates.

Well, important thing is that it's been sorted out with a different implementation.

THANKS!!

rxelizondo replied on Friday, February 19, 2010

Hi ryanlio,

Just out of curiosity, is there any particular reason why you need to cascade the root ID value of the parent down to the children?

Are you only doing that to support your CRUD operations? If so, you may want to look at this:

http://forums.lhotka.net/forums/p/8479/40326.aspx#40326

 

ryanlio replied on Friday, February 19, 2010

Hi Rene,

We are working on an existing system. The database is one where the parent PK is a string that is only generated via a SQL function upon insert. This means that we are not able to assign a PK value at the moment the object is instantiated. As the childs FK is of the same value, we therefore need the value to cascade to the child.

Hope this helps.

rxelizondo replied on Friday, February 19, 2010

hmmm, I guess I am missing something,

Why can't your Child_Insert for example, look something like this:

private void Child_Insert(Parent parent)
{
   INSERT INTO SomeTable (ForeignerKeyColumn, SomeOterColumn) VALUES (parent.A1, this.SomeVal)
}

Note how you are passing the parent reference to the funciton and note how you are getting the parent value of A1 directly form the parent reference. There is no need to store that value it on the child too.

bniemyjski replied on Friday, February 19, 2010

Hello,

The issue is that he has a non identity pk, like in the example of petshop. One thing you need to do is override the DataPortal_Create() and create a default value for your pk. Then you should just be passing down the parent to the Child_Insert and also the connection ;). You should check out how the latest nightly build of the CodeSmith CSLA templates handles this. You can actually update a non identity PK and have it cascade down to the children. I don't think its recommended but it is possible.

Thanks

-Blake Niemyjski

Copyright (c) Marimer LLC