Related entities and 1:1 relationships

Posts   
 
    
wojt
User
Posts: 20
Joined: 27-Oct-2005
# Posted on: 27-Oct-2005 06:25:03   

I may be missing something here, but I can't get the following scenario to work:

I am storing Address entities in a separate table so that I can encapsulate business logic for addresses in the AddressEntity class.

The Client entity (simplified) looks like this: Id IdAddressPostal IdAddressPhysical with 1:1 relationships on the address fields to the address entity.

The Address entity does not have any references back to Client.

When I access the ClientEntity.AddressPostal property, an empty AddressEntity is returned (correctly, as no relevant Address entity exists yet).

If I set a property on this field (say ClientEntity.AddressPostal.City) to a value, then place a watch on the entity, then immediately access that property to see if it has worked, the returned value is "", i.e. it has not been set at all.

However, if the Address entity already exists in the database, everything works correctly.

I can obviously work around this by creating the address entries manually, but is there another way?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 27-Oct-2005 11:27:08   

The empty entity returned is a 'backwards compatibility thing' from the earliest version of LLBLGen Pro, and which is to avoid object not set to an instance exceptions in your own code.

The entity returned is handed to you but not added to the graph, to avoid phantom saves (as the entity is new and if it has an identity pk it gets inserted). So, you can do two things: - in the preferences, set 'LazyLoadingWithoutResultReturnsNew' to false and regenerate your code. Now you'll get null back if Address doesn't exist. OR you can do: - add a new address entity to the Address property as you're doing now.

The default behavior of returning a new entity isn't changed during the past 2 years because it would break a lot of applications. You can however change it manually in the preference section of the designer and simply test if you receive null back.

(ps: this preference setting is new since 1.0.2005.1. Before that, you had to set properties on the entity manually to get this behavior)

Frans Bouma | Lead developer LLBLGen Pro