Creating a New Entity and Entity Relation at the same time

Posts   
 
    
jshallard
User
Posts: 62
Joined: 23-Mar-2005
# Posted on: 27-Jan-2006 10:55:53   

Release date: 12-jan-2006 Version: 1.0.2005.1.

I am using the selfservice template set. I have a customer entity and a related Address entity (each customer has an address). I create them in the following way:

//create customer
CustomerEntity customer = new CustomerEntity();

//add info to entity
customer.name = "name";
...

//add address info to related entity
customer.Address.street = "my street";
customer.Address.city= "my city";
...

//persorm recursive save
customer.Save(true);

Here is my problem: for each property that is set on the related address entity, the following seems to occur:

  • the customer entity tries to fetch address entity data from the database
  • the data isn't found as an address has not been saved yet
  • a new address entity is created
  • the property is set with the given value

However, this occurs every time an address property is set. The result of this is that all of the previous information in the address entity is lost, as a new entity is created each time. I tracked the behaviour down to the following piece of generated code (below).

Am i simply using the code incorrectly, or is this a bug?:

 /// <summary> Retrieves the related entity of type 'AddressEntity', using a relation of type 'n:1'</summary>
        /// <param name="forceFetch">if true, it will discard any changes currently in the currently loaded related entity and will refetch the entity from the persistent storage</param>
        /// <returns>A fetched entity of type 'AddressEntity' which is related to this entity.</returns>
        public virtual AddressEntity GetSingleAddress(bool forceFetch)
        {
            if( ( !_alreadyFetchedAddress || forceFetch || _alwaysFetchAddress) && !base.IsSerializing && !base.IsDeserializing )
            {

                AddressEntity newEntity = new AddressEntity();
                if(base.ParticipatesInTransaction)
                {
                    base.Transaction.Add(newEntity);
                }
                bool fetchResult = newEntity.FetchUsingPK(this.FkHomeAddressID);
                if(!_addressReturnsNewIfNotFound && !fetchResult)
                {
                    this.Address = null;
                }
                else
                {
                    if((base.ActiveContext!=null)&&fetchResult)
                    {
                        newEntity = (AddressEntity)base.ActiveContext.Get(newEntity);
                    }
                    this.Address = newEntity;
                    _alreadyFetchedAddress = fetchResult;
                }
                if(base.ParticipatesInTransaction && !fetchResult)
                {
                    base.Transaction.Remove(newEntity);
                }
            }
            return _address;
        }
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 27-Jan-2006 12:09:21   

jshallard wrote:

Release date: 12-jan-2006 Version: 1.0.2005.1.

I am using the selfservice template set. I have a customer entity and a related Address entity (each customer has an address). I create them in the following way:

//create customer
CustomerEntity customer = new CustomerEntity();

//add info to entity
customer.name = "name";
...

//add address info to related entity
customer.Address.street = "my street";
customer.Address.city= "my city";
...

//persorm recursive save
customer.Save(true);

Here is my problem: for each property that is set on the related address entity, the following seems to occur:

  • the customer entity tries to fetch address entity data from the database
  • the data isn't found as an address has not been saved yet
  • a new address entity is created
  • the property is set with the given value

However, this occurs every time an address property is set. The result of this is that all of the previous information in the address entity is lost, as a new entity is created each time. I tracked the behaviour down to the following piece of generated code (below). Am i simply using the code incorrectly, or is this a bug?:

You're setting an entity's fields which isn't there. The code in v1.x is returning a new entity if the entity isn't found, unless you've specified 'LazyLoadingWithoutResultReturnsNew' is set to false in preferences and your project's properties (which inherit from preferences at creation time).

So you should do: AddressEntity address = customer.Address; if(address.IsNew) { customer.Address = new AddressEntity(); } // set address properties.

Frans Bouma | Lead developer LLBLGen Pro
jshallard
User
Posts: 62
Joined: 23-Mar-2005
# Posted on: 27-Jan-2006 12:44:25   

Thanks.