SaveEntityCollection not refetching data into Entity

Posts   
 
    
jeffkararo
User
Posts: 76
Joined: 06-Jan-2004
# Posted on: 14-Jun-2005 19:18:40   

I have the following

entityA entityA.EntitiesB

I make some changes to the entities in the EntitiesB collection. I then perform the following:

entityA.EntitiesB = SaveEntityCollection(EntitiesB, true, false);

I get the following compile error:

Property or indexer 'entityA.EntitiesB' cannot be assigned to -- it is read only

OK, fine. Then I try the following:

SaveEntityCollection(EntitiesB, true, false);

Compiles fine and runs but I get an Out-of-sync runtime error when I want to save again. I had thought that the SaveEntityCollection would refresh the entityCollection being passed as a parm but obviously not.

Here, finally, is my question. How do I refresh the entityA.EntitiesB collection after performing the SaveEntityCollection method?

Thanks, Jeff

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 15-Jun-2005 10:54:53   

jeffkararo wrote:

I have the following

entityA entityA.EntitiesB

I make some changes to the entities in the EntitiesB collection. I then perform the following:

entityA.EntitiesB = SaveEntityCollection(EntitiesB, true, false);

I get the following compile error:

Property or indexer 'entityA.EntitiesB' cannot be assigned to -- it is read only

Please consult the reference manual for details on adapter.SaveEntityCollection(). You'll there see it doesn't return the collection saved.

OK, fine. Then I try the following:

SaveEntityCollection(EntitiesB, true, false);

Compiles fine and runs but I get an Out-of-sync runtime error when I want to save again. I had thought that the SaveEntityCollection would refresh the entityCollection being passed as a parm but obviously not.

Here, finally, is my question. How do I refresh the entityA.EntitiesB collection after performing the SaveEntityCollection method?

It does call FetchEntity afterwards, though that can fail if for example you've defined the PK field in the db as 'identity' though haven't updated the project yet, or it's a GUID and gets a new value using NEWID in the db and you really should pass in a GUID value in the client.

Could you check that for me, please?

To have fine-grained checks on this, you can manually walk entitiesB and save each entity with adapter.SaveEntity() and pass in true for refetch and false for recurse, and check the return value. If it's false, the refetch failed.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 12
Joined: 22-Nov-2005
# Posted on: 12-Dec-2005 03:46:00   

Is this the case with Adapters and version 1.0.2005.1 Final?

if it's a GUID and gets a new value using NEWID in the db and you really should pass in a GUID value in the client.

I have a data model with a RowGuid and default NEWID() as the primary key. I find that when it attempts to reload it uses a null Guid in the query, this returns nothing and everything fails. I am using the following code.

 using(DataAccessAdapter adapter = new DataAccessAdapter())
{
    adapter.SaveEntity(entity,reload);
}

I guess I should be setting the Guid in the client before hand then with Guid.NewGuid()?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 12-Dec-2005 14:53:05   

In the database: NEWID() generated values aren't read back because there's no statement in T-SQL which returns the last generated GUID by NEWID()

So you should set your PK with the GUID in code before saving.