Inserting join table row via a recursive save on a parent entity

Posts   
 
    
Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 09-May-2005 14:20:10   

Hi,

I have a join table which relates two tables A and B.

I've got a new instance of an entity from A and I've added to its collection of join table entities a new instance and set the join table entity's reference to B. The join table entity's reference to A is left unset because the entity from A to which its being added to is new and thus doesn't have a key yet.

Hope that makes sense.

Now, I was under the assumption that the join table entity added to A's collection of join table entities should have its reference to the A entity set before its inserted but I'm getting an error because its trying to insert the join table entity with a null reference to table A.

Shouldn't the A entity be being inserted and then its newly generated key be being set on the join table entities before they're inserted?

Cheers,

Ian.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 09-May-2005 19:13:46   

That should indeed be the case, though a couple of things can be wrong: 1) A doesn't have a PK 2) A has a PK, but it should be an identity / sequenced field but the identity flag isn't set/sequence isn't set or the llblgen pro project isn't updated with the identity/sequence info. 3) You've defined the PK's default value as 'NEWID()', but that value isn't read back. You have to set a GUID before saving.

Could you please check that for me, please?

Frans Bouma | Lead developer LLBLGen Pro
Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 10-May-2005 00:13:18   

Hi,

'A' does have a PK and its an identity field and its marked as one in llblGen designer and it doesn't have a NEWID() default value. 'A' entities can be inserted by themself just fine and it also works when you add join table entities to an already existing A entity although, in this case, I'm manually setting the reference to A in the join table entity before adding it to the collection.

In the situation that's not working, I examined the A entity in the debugger just before the update call. It has a pk of 0 and is set to 'IsNew'. In its collection of join row entities, the added entity has a pk of 0 and is marked as 'IsNew'.

Perhaps I'm adding the object to the collection incorrectly?

I'm just doing...

entityCollection.Add(instance);

Is that enought?

Cheers,

Ian.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 10-May-2005 09:38:29   

Ian wrote:

Hi,

'A' does have a PK and its an identity field and its marked as one in llblGen designer and it doesn't have a NEWID() default value. 'A' entities can be inserted by themself just fine and it also works when you add join table entities to an already existing A entity although, in this case, I'm manually setting the reference to A in the join table entity before adding it to the collection.

Perhaps you could post a piece of code of what you're doing, including the save call simple_smile

In the situation that's not working, I examined the A entity in the debugger just before the update call. It has a pk of 0 and is set to 'IsNew'. In its collection of join row entities, the added entity has a pk of 0 and is marked as 'IsNew'.

Perhaps I'm adding the object to the collection incorrectly?

I'm just doing...

entityCollection.Add(instance);

Is that enought?

A is 'Department', B is 'Employee'. Intermediate Entity is 'DepartmentEmployee'.


DepartmentEntity d = new DepartmentEntity();
// fill in d's properties.

EmployeeEntity e = new EmployeeEntity();
// fill  in e's properties.

DepartmentEmployeeEntity de = new DepartmentEmployeeEntity();

now either do: de.Department = d; de.Employee = e;

OR: d.DepartmentEmployees.Add(de); e.DepartmentEmployees.Add(de);

THEN, save the graph: DataAccessAdapter adapter = new DataAccessAdapter(); adapter.SaveEntity(d);

Frans Bouma | Lead developer LLBLGen Pro