IsDirty property using AddNew

Posts   
 
    
Gemsoft
User
Posts: 1
Joined: 16-Mar-2006
# Posted on: 16-Mar-2006 17:29:46   

In a Parent to child relation - Person to PhoneNumber / 1:n - when we call Person.AddressColl.Addnew() the Person's Pk is auto. entered into the Addresses's Fk field PersonID and the added AddressEntity is flagged as isDirty = true. Now when the user hasn't filled any fields into the new address entity, it will be saved anyway. Is there a build-in property to handle such a scenario? [I think that it won't even help to change the isdirty flag]

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 17-Mar-2006 02:28:55   

It should help. If the Entity IsDirty then it will be persisted to storage. If it IsNew then it will be inserted, if not then it is updated. So changing it to IsDirty = false should prevent it from being saved. If this isn't working try posting some code so we can be a little more help.

chaimweb
User
Posts: 10
Joined: 12-Apr-2005
# Posted on: 19-Mar-2006 12:53:50   

Here is my Example:

PersonContactEntity = new GSoftEntities.ContactsEntity(); GemSoft.EntityClasses.PersonEntity person = new GemSoft.EntityClasses.PersonEntity(); PersonContactEntity.Person = person; person.Address.AddNew();

As you can see, the last line creates a new address entity and sets their relation. Now I bind those entities to my Winform controls. The problem is that in case the user has not filled any of the address fields, the new address entity is still saved because, when llblgen saves the person entity, it automatically inserts the person's Id into the PersonID field of the address entity and then saves the address entity too. We even tried to capture the address.PersonIDChanged event - but the code never called that event ?? Only the address.EntityChangedEvent we were able to capture, but this means that I need to check all the fields to find out which fields were changed ! Any suggestions for an easier solution? thanks a lot, ChaimWeb

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 20-Mar-2006 10:32:56   

As you manually add the entity, the framework can't know the added entity is optional. I think the best thing you can do is this: store the address entity or entities into a collection upon save, check if these are filled in, if not, do: addressEntity.Person = null; this will remove addressEntity from Person.Address

Another way to solve this is by changing some details in your gui: the user has to click a button to ADD an address, and if the user does that, you call AddNew(). With that, the address becomes mandatory and the user therefore has to fill in the fields.

You can check whether an address is valid by adding an IEntityValidator implementation to it as the validator and simply call 'Validate()' on it.

Frans Bouma | Lead developer LLBLGen Pro