SaveEntityCollection

Posts   
 
    
jflegere
User
Posts: 33
Joined: 22-Jul-2005
# Posted on: 22-Jul-2005 22:41:46   

Hi,

I'm trying to save an entity collection like this:

EntityCollection EntitiesA = new EntityCollection...

EntityA = GetEntityA() << returns EntityA with EntityA.EntitiesB collection added >>

EntitiesA.Add(EntityA)

When I call

adapter.SaveEntityCollection(EntitiesA)

all of the EntityA entities are saved but none of the EntityB entities are saved.

BUT, when I call

adapter.SaveEntityCollection(EntitiesA, true, true)

everything is saved.

Is there a way to save everything without having to refetch?

Note: the behavior is the same whether or not there is a primary key on the EntityB table.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 23-Jul-2005 12:12:05   

jflegere wrote:

Hi,

I'm trying to save an entity collection like this:

EntityCollection EntitiesA = new EntityCollection...

EntityA = GetEntityA() << returns EntityA with EntityA.EntitiesB collection added >>

EntitiesA.Add(EntityA)

When I call

adapter.SaveEntityCollection(EntitiesA)

all of the EntityA entities are saved but none of the EntityB entities are saved.

Correct, SaveEntityCollection doesn't perform a recursive save. If you want that, use the overload to perform a recursive save.

BUT, when I call adapter.SaveEntityCollection(EntitiesA, true, true) everything is saved.

Is there a way to save everything without having to refetch?

Sure, specify 'false' for 'refetchSavedEntitiesAfterSave', the first boolean in taht overload simple_smile

Frans Bouma | Lead developer LLBLGen Pro
jflegere
User
Posts: 33
Joined: 22-Jul-2005
# Posted on: 23-Jul-2005 13:22:38   

Thanks for the quick response, Otis.

bmoeskau
User
Posts: 54
Joined: 15-Jun-2005
# Posted on: 06-Apr-2006 13:00:56   

Hmm. I have an EntityCollection of objects, each of which has an EntityCollection of sub-objects (Members containing Events). I'm writing a data conversion app that is updating all events, but I have to go through Member to get some data that will help determine how to update the event. I'm trying to do a single update at the very end so that the entire update will be treated as a single transaction, but I cannot get anything to actually update in the DB (although I get no errors -- it acts like it works). I'm trying to do this (simplified):

    EntityCollection members = new EntityCollection(new MemberEntityFactory());
    IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.MemberEntity);
    prefetchPath.Add(MemberEntity.PrefetchPathEvents);

    DataAccessAdapter daa = new DataAccessAdapter();
    daa.FetchEntityCollection(members, null, 0, null, prefetchPath);

    // loop through all events for all members and convert data

    daa.SaveEntityCollection(members, false, true);

The members themselves do not get updated (all events do), but it's my understanding that you will loop through all sub-objects looking for any that are dirty, regardless of whether or not the parent is dirty, right? Just to be sure, I tried manually setting member.IsDirty = true on each member, but that did not help.

The only way I can get it to work is to move the save into the member loop and call it after each set of events has been updated like so:

daa.SaveEntityCollection(member.Events, false, true);

This saves correctly to the DB, and will probably work for me as I will be testing it out beforehand anyway, but I would prefer a single transaction if possible. Am I missing something?

Thanks, Brian

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 06-Apr-2006 16:55:13   

What runtimeLibrary version you are using.

Also you may try to add the Events entities to be saved in a Unit Of Work object so you may save them all later in one transaction.

bmoeskau
User
Posts: 54
Joined: 15-Jun-2005
# Posted on: 06-Apr-2006 20:16:40   

SD.LLBLGen.Pro.DQE.SqlServer.NET11.dll v.1.0.20051.51207

Also you may try to add the Events entities to be saved in a Unit Of Work object so you may save them all later in one transaction.

It was my understanding that the SaveEntityCollection method automatically wrapped everything in a transaction internally for me.

bmoeskau
User
Posts: 54
Joined: 15-Jun-2005
# Posted on: 06-Apr-2006 21:45:26   

I just encountered a problem and I wonder if it's related. In running my code, I encountered an ORMEntityOutOfSyncException. The relationship between Members and Events is M:N, so I assume this means that the second time it tried to update the same event for a different member, the event in memory was out of sync with the DB version. Makes sense. I simply switched the second parameter to true to force the daa to refetch after each update and it's working now (I realize that this means that only the last update per event will win, but for my purposes, this is fine).

I thought maybe that had something to do with my original problem, so I tried running the original update on the members collection setting the refetch parameter to true, but it still did not work. However, I did want to mention the M:N relation if that might have anything to do with it. Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 07-Apr-2006 09:25:54   

SD.LLBLGen.Pro.DQE.SqlServer.NET11.dll v.1.0.20051.51207

Would you please try it out with the latest version of the RuntimeLibraries?

bmoeskau
User
Posts: 54
Joined: 15-Jun-2005
# Posted on: 09-Apr-2006 11:52:02   

I upgraded but that did not solve the problem. It is now academic to me as I am just saving each sub-collection separately which works for what I need, but I would still be interested in figuring out why it's not working at the root level, esp. if I'm doing something wrong. But I don't have time to dwell on it at the moment. disappointed

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 09-Apr-2006 12:14:46   

bmoeskau wrote:

I upgraded but that did not solve the problem. It is now academic to me as I am just saving each sub-collection separately which works for what I need, but I would still be interested in figuring out why it's not working at the root level, esp. if I'm doing something wrong. But I don't have time to dwell on it at the moment. disappointed

The subcollection is that an M:N collection? Also are there any relations hidden between the entities involved?

the fact is that if you have this setup: A <- B -> C which means that A m:n C, you'll have a A.Cs collection. Now, if you add a new C to A.Cs, it's not saved, as its not considered in the save because m:n relations are read-only. Do: A.Bs[index].C = c;

or: b.A = a; b.C = c;

save A.

I'll look into your problem again to see if I misunderstood it.

(edit). You describe that you have to update events. I then don't understand why members have to be updated as well, as you update events?

Anyway, you should fetch with a prefetch path members via the intermediate entity, thus Member.MemberEvents.Events should be the graph, then walk for each member, the MemberEvent objects and update the referenced Event object.

Frans Bouma | Lead developer LLBLGen Pro