New entity with default value

Posts   
 
    
Barry
User
Posts: 232
Joined: 17-Aug-2005
# Posted on: 20-Sep-2005 10:42:53   

I'm using adapter model, I want to set default value for some properties (such as created date) of the newly created entity. I implement this in InitClassEmpty(), it works fine for new entities, and default value assigned can be saved to database too.

However, those properties with default value assigned are flagged as changed, when I fetch existing entities from database, those properties is not set to the value read from database, but assigned default value. For example, created date is updated everytime I fetch the entities, I cannot get the correct value from database.


private void InitClassEmpty(ItemValidator validator, IEntityFields2 fields)
{
    InitClassMembers();

    base.Fields = fields;
    base.IsNew=true;
    base.Validator = validator;

    // __LLBLGENPRO_USER_CODE_REGION_START InitClassEmpty
    CreatedDate = DateTime.Now;
    // __LLBLGENPRO_USER_CODE_REGION_END

}

Am I coding in wrong ways to set default value? Thank you very much!

Walaa avatar
Walaa
Support Team
Posts: 14954
Joined: 21-Aug-2005
# Posted on: 20-Sep-2005 11:21:46   

I would have set those default values directly in the database.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39625
Joined: 17-Aug-2003
# Posted on: 20-Sep-2005 11:27:23   

Additionally, if you want to do it in code instead of with default constraints as Walaa suggested, change:


    // __LLBLGENPRO_USER_CODE_REGION_START InitClassEmpty
    CreatedDate = DateTime.Now;
    // __LLBLGENPRO_USER_CODE_REGION_END

into:


    // __LLBLGENPRO_USER_CODE_REGION_START InitClassEmpty
if(fields.State==EntityState.New)
{
    CreatedDate = DateTime.Now;
}
    // __LLBLGENPRO_USER_CODE_REGION_END

Frans Bouma | Lead developer LLBLGen Pro
Walaa avatar
Walaa
Support Team
Posts: 14954
Joined: 21-Aug-2005
# Posted on: 20-Sep-2005 11:41:08   
Barry
User
Posts: 232
Joined: 17-Aug-2005
# Posted on: 20-Sep-2005 12:08:14   

It works great! Thanks all of you!!! smile

Posts: 134
Joined: 04-Mar-2005
# Posted on: 20-Sep-2005 19:17:39   

If you're still looking for some example code I did this same thing. My code fills the default values on the client-side when adding a new record through the GUI and also verifies that default values are filled on the server side before saving to enure that programmatic adds are handled. Both use the same default value definition.

If you're interested in the code email at simon-underscore-beets-at-adp-dot-com.