Emmanuel wrote:
From that thread I see that the poster suggested doing the following to 'reset' an existing, used Entity object so that it could be used again:
- entity1.Isnew = True
- set all fields of entity1
- mark all entity1' fields as Changed
I understand step 1 and step 2. And I think I understand step 3 although I didn't know that one had to do this to ensure that fields can persisted. Does one?
Also, what about related entities held by an Entity object. I gather I would need to clear out those collections. Is that as simple as just doing Entity.RelatedCollection.Clear() or do I need to also set some other properties too?
If you save your entity without refecht, then in step 1 you also have to set entity.Fields.State = New to avoid the OutSyncException.
Really step 3 should be replace as follow:
Before set all entity's field to the new value, all the entity's field = defaultValue and set field as not changed. I would like the framework to have a routine to do all this.
I did step 3 because as I was migrating data from one database to other, if the value that you assign to a field is equal the last assigned value, then the field is not flaged as changed.
About the relatedCollection, I was getting a hard time trying to save an entity and its related entities automatically; but I did not get success. The first time the related entity get its fk assigned automatically from the parent entity pk (identity field); but after that the related entity did not get the fk assigned, I assigned 0 the the related entity's field that related it with the parent entity with the idea to break the link between entities; but that did not help. May be I was doing something wrong, may be I did not set the right property.
At the end I saved the parent entity with refetch, then I assigned the parent primary key (identity field) to the related entitty and saved it.
May be Frans can help with a routine of the steps necesary to put an entity as new without creating it.