This is a migrated thread and some comments may be shown as answers.

Foriegn Key Relationship Error

1 Answer 58 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Majid
Top achievements
Rank 2
Majid asked on 13 Jul 2009, 07:35 AM
Hi

I am using Telerik ORM to insert two objects. Object1 primaryKey(Guid or uniqueidentifie) is forigen key in Object2. When i save these objects, then Object2 is raising exception for conflict in PrimaryKey_ForiegnKey relationship. Primary key is provided at run time using Guid.

this is code
conn.StartTransaction();   
 
object value = conn.ExcuteNonQuery(transferWay);   
   
if (transferCharge != null && value!=null) {  
 
transferCharge.TransferWay = transferWay.TransferWayID;  
 
value = conn.ExcuteNonQuery(transferCharge);  
 
}  
conn.EndTransaction();  
 
   
 
 
 

Error is

Insert of '1039162118-03b633b7-18c1-4450-9113-5617229eae1c' failed: Telerik.OpenAccess.RT.sql.SQLException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_TransferCharges_TransferWay1". The conflict occurred in database "DB", table "dbo.TransferWay", column 'TransferWayID'.

 How should i solve this problem?

 

1 Answer, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 14 Jul 2009, 07:39 AM
Hello Majid,

As far as I can see, you are working directly with the Object1 ID in Object2. If you were using a reference in Object2 to Object1, OpenAccess would be able to recognize the relationship and would store first Object1 and then Object2. This would not break the constraint. However, in your case maybe OpenAccess first tries to insert Object2 and then Object1. To guarantee the correct insert order, you can use two transactions. Open a transaction, insert Object1 and commit the changes. Then open another transaction, insert Object2 and commit again. Hope this will solve the problem.

Best wishes,
Alexander
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
General Discussions
Asked by
Majid
Top achievements
Rank 2
Answers by
Alexander
Telerik team
Share this question
or