Problems with SaveEntity with recurse

Posts   
 
    
knez
User
Posts: 37
Joined: 01-Nov-2004
# Posted on: 16-Dec-2004 16:04:58   

Hi!

I am currently working by Adapter scenario (using LLBLGen Pro version 1.0.2004.Final released on Septermber, 24th) on a project that has tables as following:

LeasingAgreement: LeasingAgreementId (PK), ...

AgreementObjectType AgreementObjectTypeId (PK), ...

AgreementObjectTypeAttribute AgreementObjectTypeAttributeId (PK), AgreementObjectTypeId (FK to AgreementObjectType table), ...

AgreementObject: AgreementObjectId (PK), AgreementObjectTypeId (FK to AgreementObjectType table), ...

AgreementObjectAttributeValue: AgreementObjectAttributeValueId (PK) AgreementObjectId (FK to AgreementObject table), AgreementObjectTypeAttributeId (FK to AgreementObjectTypeAttribute table), AgreementObjectAttributeValue (varchar), ...

LeasingAgreementObject: LeasingAgreementObjectId (PK), LeasingAgreementId (FK to LeasingAgreement table), AgreementObjectId (FK to AgreementObject table)

This sets LeasingAgreement:AgreementObject as m:n.

In my code I am creating LeasingAgreement object with subordinate objects in following code:

            LeasingAgreementEntity agreement = new LeasingAgreementEntity();
            ...

            AgreementObjectEntity object1 = new AgreementObjectEntity();
            object1.AgreementObjectTypeId = 1;

            AgreementObjectAttributeValueEntity value1 = new AgreementObjectAttributeValueEntity();
            value1.AgreementObjectTypeAttributeId = 1;
            value1.AgreementObjectTypeAttributeValue = "value1";

            AgreementObjectAttributeValueEntity value2 = new AgreementObjectAttributeValueEntity();
            value2.AgreementObjectTypeAttributeId = 2;
            value2.AgreementObjectTypeAttributeValue = "value2";

            agreement.AgreementObject.IsReadOnly = false; // if I don't do this I get InvalidOperationException.
            object1.AgreementObjectAttributeValue.Add(value1);
            object1.AgreementObjectAttributeValue.Add(value2);
            agreement.AgreementObject.Add(object1);

            new DataAccessAdapter().SaveEntity(agreement, true, null, true);

After save, only LeasingAgreement table has inserted data and none of the others. Where is the problem? I know there were some hotfixes (I haven't installed) after the release I am using, but I am not sure that any is fixing this type of problem. I also know that were discussions on similar subject, but I still don't know how to solve this problem. I haven't hidden any of relations, so I could save entity like this, right?

Please help! It's very urgent.

Thanks.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 16-Dec-2004 19:12:35   

m:n relations aren't readonly for nothing. simple_smile You can't save entities added to them. The reason for that is that m:n relations can be using an intermediate entity which contains non-pk values, like customer - employee (m:n) which is based on order. adding a new employee to customer.Employees should then save a new order object as well, but that's not possible without supplying information.

So to solve this, you have to add the m:n related entity via a new intermediate entity. So create the intermediate entity, assign both its FK fields to the 2 m:n related entities and it should be ok.

Frans Bouma | Lead developer LLBLGen Pro