Foriegn Key Relationship Error

Thread is closed for posting
2 posts, 0 answers
  1. Majid
    Majid avatar
    9 posts
    Member since:
    Dec 2008

    Posted 13 Jul 2009 Link to this post


    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
    object value = conn.ExcuteNonQuery(transferWay);   
    if (transferCharge != null && value!=null) {  
    transferCharge.TransferWay = transferWay.TransferWayID;  
    value = conn.ExcuteNonQuery(transferCharge);  

    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?


  2. Alexander
    Alexander avatar
    727 posts

    Posted 14 Jul 2009 Link to this post

    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,
    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.
Back to Top