Empty Child Entities

Posts   
 
    
asowles
User
Posts: 46
Joined: 23-Apr-2008
# Posted on: 22-Jun-2009 21:43:31   

We are having some trouble with empty child entities. We are using this code to populate an EmployeeEntity instance which has an optional EmployeeRules Entity



public EntityCollection<EmployeeEntity> FetchEmployees(bool withChildren)

        {

            EntityCollection<EmployeeEntity> employees = new EntityCollection<EmployeeEntity>(new EmployeeEntityFactory());

            IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.EmployeeEntity);

            RelationCollection rulesCollection = new RelationCollection();



            rulesCollection.Add(EmployeeEntity.Relations.EmployeeRulesEntityUsingCompanyIdEmployeeId, JoinHint.Left);

            if (withChildren)

            {

                prefetchPath.Add(EmployeeEntity.PrefetchPathDepartment);

                prefetchPath.Add(EmployeeEntity.PrefetchPathEmployeeRules, 0, null, rulesCollection);

                prefetchPath.Add(EmployeeEntity.PrefetchPathPayclass);

                prefetchPath.Add(EmployeeEntity.PrefetchPathEmployeeTypes);

                prefetchPath.Add(EmployeeEntity.PrefetchPathEmployeeBenefits).SubPath.Add(EmployeeBenefitEntity.PrefetchPathPaycode);

                

            }



            adapter.FetchEntityCollection(employees, null, prefetchPath);



            return employees;

        }

The problem that we are having is that the EmployeeEntity is created with an empty EmployeeRules Entity and not null. This causes problems because when we call to save the EmployeeEntity, the EmployeeRules entity is not valid and it generates errors.

How do we prevent it from creating a blank, invalid entity instead of null?

Allen Sowles

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 23-Jun-2009 03:05:05   

I've had this same problem in Self-Servicing. It's a blessing & a curse at the same time. In Self-Servicing its worse, because you expect the Entity to simply lazy-load & be there... but it's really not. Returning a New Entity can be more of a curse - and it can be very hard to backtrace & determine the problem. Glad you've determined the problem.

In the Designer, please look at your Options. There is an option for LazyLoadingReturnsNewIfNotFound. Make sure you turn this OFF. You will now get NULL for an entity instead of a New Entity.

Hope this helps!

Ryan

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Jun-2009 04:34:35   

Hi Allen

asowles wrote:

The problem that we are having is that the EmployeeEntity is created with an empty EmployeeRules Entity and not null. This causes problems because when we call to save the EmployeeEntity, the EmployeeRules entity is not valid and it generates errors.

Please elaborate more on "is not valid and generate errors".

asowles wrote:

How do we prevent it from creating a blank, invalid entity instead of null?

As a matter of fact, any m:1/1:1 related entity will be accessible as NULL if no related entity exists. What LLBLGen build version and runtime library version are you using? (http://llblgen.com/TinyForum/Messages.aspx?ThreadID=7725)

rdhatch wrote:

In the Designer, please look at your Options. There is an option for LazyLoadingReturnsNewIfNotFound. Make sure you turn this OFF. You will now get NULL for an entity instead of a New Entity.

This flag is SelfServicing specific, Allen is using Adapter.

BTW...

rulesCollection.Add(EmployeeEntity.Relations.EmployeeRulesEntityUsingCompanyIdEmployeeId, JoinHint.Left);
...
prefetchPath.Add(EmployeeEntity.PrefetchPathEmployeeRules, 0, null, rulesCollection);

You don't need to do that (add a relationcollection to the path), unless you pass a PredicateExpression to the prefetchPath.

David Elizondo | LLBLGen Support Team
rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 23-Jun-2009 14:44:40   

Thanks for the correction, David. I wasn't 100% positive the ReturnNewIfNotFound flag effected adapter.

Ryan

asowles
User
Posts: 46
Joined: 23-Apr-2008
# Posted on: 24-Jun-2009 17:37:21   

Thank you for the messages. I apologize for not including all of the relavent data. I am using LLBLGen 2.6 with runtime 2.0.50727. I am using Adapter with Sybase iAnywhere. Here is the scenario:

We have an Employee table and an EmployeeRules table. The EmployeeRules is an optional record. The Employee table has a nullable foreign key to the EmployeeRules table. When we call for the EmployeeEntity we prefetch the child entites, one of which is the EmployeeRules entity. We expected the EmployeeRules entity to be null if it didn't exist, but it is not null. It creates a blank entity (it's there, but doesn't have any values). The primary key is set to -0- (it is an int in the database) so when we try to save the EmployeeEntity and it attempts to save the children, it fails because the entity isn't valid.

We have tried using both option (JoinHint and obeyWeakRelations) but both create the empty entity.

How do I prevent it from creating the blank entity?

Thanks,

Allen

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-Jun-2009 05:09:56   

Hi Allen,

I can't reproduce that. If the related record (m:1 or 1:1 relation) doesn't exist, the related entity should be null. First please update to the latest runtime library version (the one you posted doesn't seem to be a 2.6 RTL version.

David Elizondo | LLBLGen Support Team