Polymorphic fetch with business entities

Posts   
 
    
wtijsma
User
Posts: 252
Joined: 18-Apr-2006
# Posted on: 12-Dec-2007 19:12:35   

When I attempt to fill an EntityCollection with a polymorphic entity from the database, I get a System.ArgumentException:

An exception of type 'System.ArgumentException' occurred in SD.LLBLGen.Pro.ORMSupportClasses.NET20.DLL but was not handled in user code

The error message is: entityToAdd isn't of the right type.

EntityCollection<Contract> list = new EntityCollection<Contract>(new ContractFactory());
Contract addNew = list.AddNew();
_daa.FetchEntityCollection(list, null);

ContractFactory looks like this:

    public class ContractFactory : LeaseContractEntityFactory
    {

        public override IEntity2 Create()
        {
            return new Contract();
        }

        public override IEntity2  Create(IEntityFields2 fields)
        {
            return new Contract(fields);
        }
    }

"Contract" is a custom class that inherits from "LeaseContractEntity", and "LeaseContractEntity" is a generated class that inherits from "ContractEntity".

I've added a partial class with ExecuteMultiRowRetrievalQuery to DataAccessAdapter, and the error occurs here:

    
public partial class DataAccessAdapter : DataAccessAdapterBase
    {
        public override void ExecuteMultiRowRetrievalQuery(IRetrievalQuery queryToExecute, IEntityFactory2 entityFactory, IEntityCollection2 collectionToFill, IFieldPersistenceInfo[] fieldsPersistenceInfo, bool allowDuplicates, IEntityFields2 fieldsUsedForQuery)
        {
            base.ExecuteMultiRowRetrievalQuery(queryToExecute, entityFactory, collectionToFill, fieldsPersistenceInfo, allowDuplicates, fieldsUsedForQuery);
        }
    }

Any ideas?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 13-Dec-2007 10:51:53   

EntityCollection<Contract> list = new EntityCollection<Contract>(new ContractFactory()); Contract addNew = list.AddNew(); _daa.FetchEntityCollection(list, null);

ContractFactory looks like this:

public class ContractFactory : LeaseContractEntityFactory
{

    public override IEntity2 Create()
    {
        return new Contract();
    }

    public override IEntity2 Create(IEntityFields2 fields)
    {
        return new Contract(fields);
    }
}

"Contract" is a custom class that inherits from "LeaseContractEntity", and "LeaseContractEntity" is a generated class that inherits from "ContractEntity".

Would you please try the following"

EntityCollection<LeaseContractEntity> list = new EntityCollection<LeaseContractEntity>(new ContractFactory());
wtijsma
User
Posts: 252
Joined: 18-Apr-2006
# Posted on: 13-Dec-2007 11:23:35   

Walaa wrote:

Would you please try the following"

EntityCollection<LeaseContractEntity> list = new EntityCollection<LeaseContractEntity>(new ContractFactory());

Thanks, then the query works, however all the entities are of type "LeaseContractEntity, so my factory seems to be ignored.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 13-Dec-2007 12:34:49   

What about the following?

EntityCollection<Contract> list = new EntityCollection<Contract>();
Contract addNew = list.AddNew();
_daa.FetchEntityCollection(list, null);

Which LLBLGen Pro runtime library version are you using?

wtijsma
User
Posts: 252
Joined: 18-Apr-2006
# Posted on: 13-Dec-2007 12:53:11   

Walaa wrote:

What about the following?

EntityCollection<Contract> list = new EntityCollection<Contract>();
Contract addNew = list.AddNew();
_daa.FetchEntityCollection(list, null);

Which LLBLGen Pro runtime library version are you using?

Hi,

Yes this works correctly, a Contract instance is added to the list.

Assemblies I'm using:

SD.LLBLGen.Pro.ORMSupportClasses.NET20: 2.5.0.0
SD.LLBLGen.Pro.DQE.SqlServer.NET20: 2.5.0.0

wtijsma
User
Posts: 252
Joined: 18-Apr-2006
# Posted on: 14-Dec-2007 17:25:38   

Hi Walaa,

Any news on this issue?

Thanks...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 15-Dec-2007 09:50:30   

wtijsma wrote:

Hi Walaa,

Any news on this issue?

Thanks...

You reported it works, or is it not working?

Frans Bouma | Lead developer LLBLGen Pro
wtijsma
User
Posts: 252
Joined: 18-Apr-2006
# Posted on: 15-Dec-2007 16:48:56   

Otis wrote:

wtijsma wrote:

Hi Walaa,

Any news on this issue?

Thanks...

You reported it works, or is it not working?

No, the calling collection.AddNew() on the collection uses the factory and adds the right entity, however a database fetch doesn't...

Posts: 254
Joined: 16-Nov-2006
# Posted on: 16-Dec-2007 00:03:59   

Can you explain further

database fetch doesn't...

What exactly does it do?

wtijsma
User
Posts: 252
Joined: 18-Apr-2006
# Posted on: 16-Dec-2007 04:02:54   

MattAdamson wrote:

Can you explain further

database fetch doesn't...

What exactly does it do?

Well, if I create an instance of EntityCollection<Contract> with my ContractFactory it will actually try to create instances of the base class of Contract, LeaseContractEntity, causing the error mentioned earlier.

Which means the factory is actually ignored, maybe because it's a polymorphic fetch and it uses it's own factory.

When I call 'AddNew' on the collection, it acually adds the right entity (Contract), and uses the factory.

The same behaviour applies when I declare the collection als EntityCollection<LeaseContractEntity> with my own ContractFactory.

I've generated using the 'Two Classes' adapter templates first, but was unable to change the class name easily without altering the templates (somehow it always started with 'My' even though I changed the settings for the Task Performer), so decided to use my own inherited classes.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 17-Dec-2007 10:17:07   

however a database fetch doesn't...

Still haven't explained this part.

What happens on the fetch routine? Would you please post the generated SQL query? Consult the LLBLGen Pro manual's section "Using the generated code -> Troubleshooting and debugging", to know how to get the generated SQL Queries.

wtijsma
User
Posts: 252
Joined: 18-Apr-2006
# Posted on: 27-Dec-2007 16:43:05   

edit: Moved to the helpdesk part to protect my customers names http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=12208