multiple FKs to the same table problem

Posts   
 
    
OiNk!
User
Posts: 12
Joined: 13-Mar-2008
# Posted on: 13-Mar-2008 13:39:53   

Hello,

S.O.S... flushed

I have 2 tables, Slip and Slipassociate. Relationship is...

Slip.SlipId to SlipAssociate.SlipId and Slip.SlipId to SlipAssociate.AssociateSlipId, i.e. both IDs on the associate link back to the same field on slip. Both the slipAssociate FK's do not allow NULLS.

This happens in a UOW:

SlipassociateEntity slipassociateEntity = new SlipassociateEntity(); slipassociateEntity.Slipid = slip.SlipcodeId; slipassociateEntity.AssociateSlipid = slip.SlipcodeId; slip.Slipassociate.Add(slipassociateEntity);

You don't need to set the FK value (which is SlipAssociateEntity.SlipId above) manually as it will automatically be set by LLBLGen, right? (Due to relation)

Problem is I am getting an "INSERT statement conflicted with FK constraint"?

Generated Sql query: Query: INSERT INTO [company].[dbo].[tblSLIP] ([CompanyID], [SLIPCode], [SLIP], [GLCodeID], [SLIPTypeID], [SLIPScopeID], [SLIPCost], [SLIPPercent], [TaxId], [CCMID], [MinCalcCost], [MinCalcPercent], [PrintNoticeCost], [BudgetBillOptionID], [IsActive], [UserAltered], [EffectiveDate], [ExpiryDate]) VALUES (@CompanyId, @Slipcode, @Slip, @GlcodeId, @SliptypeId, @SlipscopeId, @Slipcost, @Slippercent, @TaxId, @Ccmid, @MinCalcCost, @MinCalcPercent, @PrintNoticeCost, @BudgetBillOptionId, @IsActive, @UserAltered, @EffectiveDate, @ExpiryDate);SELECT @SlipcodeId=SCOPE_IDENTITY() Parameter: @SlipcodeId : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Output. Value: <undefined value>. Parameter: @CompanyId : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 1. Parameter: @Slipcode : String. Length: 6. Precision: 0. Scale: 0. Direction: Input. Value: "111111". Parameter: @Slip : String. Length: 25. Precision: 0. Scale: 0. Direction: Input. Value: "1111111111111111111111111". Parameter: @GlcodeId : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 4. Parameter: @SliptypeId : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 2. Parameter: @SlipscopeId : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 2. Parameter: @Slipcost : Decimal. Length: 0. Precision: 15. Scale: 5. Direction: Input. Value: 2. Parameter: @Slippercent : Decimal. Length: 0. Precision: 11. Scale: 5. Direction: Input. Value: 3. Parameter: @TaxId : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 32. Parameter: @Ccmid : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 2. Parameter: @MinCalcCost : Decimal. Length: 0. Precision: 15. Scale: 5. Direction: Input. Value: 5. Parameter: @MinCalcPercent : Decimal. Length: 0. Precision: 11. Scale: 5. Direction: Input. Value: 5. Parameter: @PrintNoticeCost : Decimal. Length: 0. Precision: 15. Scale: 5. Direction: Input. Value: 5. Parameter: @BudgetBillOptionId : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 1. Parameter: @IsActive : Boolean. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: True. Parameter: @UserAltered : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 3. Parameter: @EffectiveDate : DateTime. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 13/03/2008 11:46:26 AM. Parameter: @ExpiryDate : DateTime. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 13/03/2008 11:46:26 AM.

...followed by...

Generated Sql query: Query: INSERT INTO [company].[dbo].[tblSLIPAssociate] ([SLIPID], [AssociateSLIPID]) VALUES (@Slipid, @AssociateSlipid);SELECT @SlipassociateId=SCOPE_IDENTITY() Parameter: @SlipassociateId : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Output. Value: <undefined value>. Parameter: @Slipid : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 54. Parameter: @AssociateSlipid : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 0.

It is setting the Slipid correctly, but the AssociateSlipid which should be set in exactly the same way is being sent through as 0 for some reason?

slipassociateEntity.Slipid = slip.SlipcodeId; works, i.e. it seems to know that if it is new and even with me setting that value it should still link the FKs properly? Note, it ll happens in a UOW.

It is only failing on the second one which makes me think there might be a problem with multiple FKs to the same table?

Any help is much appreciated...

Keep up all the good work!

OiNk!
User
Posts: 12
Joined: 13-Mar-2008
# Posted on: 13-Mar-2008 14:08:02   

Hi,

Sorry, I forgot to mention, but the following information might assist with my query:

LLBLGen Pro version: 2.5.8.228 Template: Adapter .NET 2.0 Database: SQL Server 2005

Thank you!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 13-Mar-2008 14:46:41   

LLBLGen Suncronize the PK-FK when you associate entities to each other. Like: myOrder.Customer = myCustomer.

Inserting this graph, will first Insert the customer then it will insert the Order after setting the myOrder.CustomerId to the returned Id of the inserted customer.

So going back to your case:

SlipassociateEntity slipassociateEntity = new SlipassociateEntity(); slipassociateEntity.Slipid = slip.SlipcodeId; slipassociateEntity.AssociateSlipid = slip.SlipcodeId; slip.Slipassociate.Add(slipassociateEntity);

LLBLGen Inserts the slip entity first. Then it attempts to insert the slipassociateEntity which has 2 FKs to the slip entity. LLBLGen will succeed in synchronizing the first FK. Because you have specified: slip.Slipassociate.Add(slipassociateEntity);

And slipassociateEntity.AssociateSlipid will be set to NULL because it was set to slip.SlipcodeId which was NULL at that time.

In the SlipEntity you should have 2 fields mapped on the 2 relations with the SlipassociateEntity, So what you should do to sync both FKs is:

SlipassociateEntity slipassociateEntity = new SlipassociateEntity();

slip.Slipassociate.Add(slipassociateEntity);
slip.Slipassociate_.Add(slipassociateEntity);

Note: Slipassociate_ might have another name, you should check it out in the LLBLGen Pro Designer.

OiNk!
User
Posts: 12
Joined: 13-Mar-2008
# Posted on: 13-Mar-2008 15:02:53   

Walaa you're a star... that worked. smile

Thanks a mil.