[SOLVED] Problems with recursive save

Posts   
 
    
Jeff M
User
Posts: 250
Joined: 04-Aug-2004
# Posted on: 06-Jan-2005 23:55:15   

SQL Server 2000. Adapter. Parent entity is "AccountEntity" (PK Guid AccKey), children entities "ContactEntity" (PK Guid AccKey AND PK int ConKey). Create a new AccountEntity and manually enter the PK in code, because its a Guid: (ent.AccKey = Guid.NewGuid). Create several new Contact entities via datagrid.

Save AccountEntity with:

AccessAdapter adapter = new AccessAdapter() ;
adapter.SaveEntity( this.ent, true, null, true) ;
  • EXCEPTION * - > Get error message indicating that Contact table field AccKey cannot accept a NULL value.

I modify the aboved-shown code to:

foreach (Contact c in ((Account)entity).Contacts)
{
         c.AccKey = ((Account)entity).AccKey ;
}
AccessAdapter adapter = new AccessAdapter() ;
adapter.SaveEntity( this.entity, true, null, true) ;

... and now it works. But, I thought that LLBLGenPro was supposed to automatically enter key values in the children records based on the Parent key value. It appears that I have to help it along.

Am I doing something wrong here?

Thank, Frans.

Jeff

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 07-Jan-2005 09:30:24   

What does SaveEntity() return? False? Your setup seems to be very basic and should work so I'm a bit confused why it fails. The PK-FK syncing is performed after the pk side is saved, and it then ends up in OnEntityAfterSave() in the FK side, in your case the ContactEntity. Could you check via a breakpoint if that's the case?

(Devildog: I removed your post)

Frans Bouma | Lead developer LLBLGen Pro
Jeff M
User
Posts: 250
Joined: 04-Aug-2004
# Posted on: 07-Jan-2005 20:58:45   

First of all, I made a mistake...

The parent entity is 'Account", not the 'AccountEntity' that is generated by LLBL. 'Account' inherits 'AccountEntity'. Same with 'Contact' and 'ContactEntity'.

AccessAdapter adapter = new AccessAdapter() ;
adapter.SaveEntity( this.ent, true, null, true) ;

... this.ent is an instance of 'Account'. This call generates the SQL Server error that says that AccKey in the Contact table cannot be null.

I can't test the result of:

AccessAdapter adapter = new AccessAdapter() ;
if (adapter.SaveEntity( this.entity, true, null, true))
{ code... } else { code... }

because adapter.SaveEntity (this.entity, true, null, true) triggers the exception.

Placing a breakpoint within protected virtual void OnEntityAfterSave(object sender, EventArgs e) in ContactEntity (the ancester to Contact), indicates that this method is never called.

Jeff

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 07-Jan-2005 21:41:47   

Ok, how did you define Account? The problem can be that the Collection of contacts is defined as an IList. Adding a new account will then add the contact using the collectionbase.add method, bypassing the EntityCollectionBase2.Add method which sets up all kinds of things.

For example, if you add a new contact instance to Account.Contacts, by calling account.Contacts.Add(), do you end up in ContactEntity.SetRelatedEntity() ?

Frans Bouma | Lead developer LLBLGen Pro
Jeff M
User
Posts: 250
Joined: 04-Aug-2004
# Posted on: 07-Jan-2005 21:58:04   

Well, Account directly inherits the MyAccountEntity that is created by using the 'Adapter scenario with subclasses' generator.

After I create an empty Account entity (note: there is no call to DataAccessAdapter when I do this, so, obviously, there are no prefetches, either). I then manually set ((Account)entity).AccKey to Guid.NewGuid.

That is the parent entity. I use an Infragistics UltraWinGrid to populate the Contacts collection by setting contactsGrid.DataSource = ((Account)entity).Contacts.

Does this help?

Jeff

Jeff M
User
Posts: 250
Joined: 04-Aug-2004
# Posted on: 07-Jan-2005 22:18:38   

By the way: ContactEntity.SetRelatedEntity only returns the default:


public override void SetRelatedEntity(IEntity2 relatedEntity, string fieldName)
{
switch(fieldName)
{

default:
// do nothing
break;
}
}

No reference to 'Account'. I think that we may be on to something here.

Jeff

Jeff M
User
Posts: 250
Joined: 04-Aug-2004
# Posted on: 07-Jan-2005 22:38:49   

I HAD THE CONTACT-ACCOUNT ENTITY HIDDEN IN THE DESIGNER SO, IT WAS NEVER CREATED!!!!! I have NO idea why I did that!

In the immortal words of the Wedding Singer...

I'M SUCH AN IDIOT!

Anyway, everything works now. Thanks Frans. It was your SetRelatedEntity clue that did it.

Jeff

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 08-Jan-2005 12:31:28   

hehe simple_smile Glad it's solved! simple_smile

Frans Bouma | Lead developer LLBLGen Pro