Initial values for entity fields

Posts   
 
    
wvnoort
User
Posts: 96
Joined: 06-Jan-2005
# Posted on: 20-May-2005 09:35:07   

I think I misunderstood the purpose of the InitClassEmpty method. There is a nice user code region, so i thought i could use it to set initial values for the fields of the entity, like this:


// __LLBLGENPRO_USER_CODE_REGION_START InitClassEmpty
if (Fields.State == EntityState.New)
{
    percentage = 100;
    amount = 0;
}
// __LLBLGENPRO_USER_CODE_REGION_END

However, when i fetch a collection of entities from the DB the values from the DB are overwritten by my settings in the user code region. It was supposed to do that only for new entities. confused

Why does this happen and what is the better way of supplying initial values?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39863
Joined: 17-Aug-2003
# Posted on: 20-May-2005 10:55:54   

It appears to be this piece of code in the object fetch routine:


while(dataSource.Read())
{
    IEntityFields2 newFields = entityFactory.CreateFields();
    ReadRowIntoFields(dataSource, newFields, fieldsPersistenceInfo, ref fieldIndexToOrdinal);

    IEntity2 entityToAdd = entityFactory.Create(newFields);
    if(validatorToUse!=null)
    {
        entityToAdd.Validator = validatorToUse;
    }

    entityToAdd.Fields.State = EntityState.Fetched;
    entityToAdd.IsNew=false;

As you can see, the state is set after the entity's created and the fields are set. I'll reschedule this now, and will upload a hotfix for the runtime libraries so your code should work (as the way you're using it is the way it should be used).

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39863
Joined: 17-Aug-2003
# Posted on: 20-May-2005 12:30:01   

A hotfix for this is now available (hotfix for runtime libraries in the runtime library section).

Frans Bouma | Lead developer LLBLGen Pro
wvnoort
User
Posts: 96
Joined: 06-Jan-2005
# Posted on: 24-May-2005 12:46:52   

Thanks for your quick fix. Now it's working as expected simple_smile