Scope Identity

5 posts, 0 answers
  1. Kareem
    Kareem avatar
    7 posts
    Member since:
    Nov 2008

    Posted 05 Mar 2009 Link to this post


    My question is in regards to how Open Access is handling identity colums with tables that have foreign references.

    Say For example I have the following Persistent classes:

    Public Class Tmp  
        Private Dim _id As Integer  ' pk identity field auto increment  
        Private Dim _name As String   
        Private Dim _tmpchild As IList(OF TmpChild) = New List(OF TmpChild)()   
    End Class  
    Class TmpChild  
        Private Dim _tmpjobid As Integer  ' pk   
        Private Dim _value As String   
        Private Dim _tmp As Tmp  ' inverse Tmp._tmpChild  
    End Class 

    Is it possible to create a new Tmp object and add TmpChild object to the newly created in one Transaction?




           parent  = New Tmp  
          child = New TmpChild

         parent.Name = "John Joe"
         parent.children.Add(child)  ' internally the new parent id would be assigned then should be used in adding the child object




        Catch e As Exception

        End Try

    This creates a foreign key error because the id does not exist when adding.

    Or do we need to get the identity value returned then add the child values? This creates a problem because if the parent insert was successful and the child insert failed It requires that the parent insert now be deleted.

    What's the best way to accomplish this?

  2. Zoran
    Zoran avatar
    534 posts

    Posted 07 Mar 2009 Link to this post

    Hi Kareem,

    When inserting/updating objects in a 1:m relations you should only do the desired operation only for the root object in the object graph. OpenAcess will take care of the referenced objects from the collection and it will insert/modify the records in their corresponding table and join tables if required.

    For clarification, in your example you should only call scope.Add(parent) and the child object which is part of the children collection will be inserted automatically.

    I hope my response provides helpful for you. If you still have any problems with OpenAccess please do not hesitate to contact us again in the future.

    Sincerely yours,
    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.
  3. SuperXRAY
    SuperXRAY avatar
    82 posts
    Member since:
    Feb 2007

    Posted 18 Mar 2009 Link to this post

    I would like to see this elaborated on. I have tried several approaches and neither have seemed to work. I have projects with 1:m and 1:1 relationships, but OpenAccess is trying to insert a NULL value.
  4. Zoran
    Zoran avatar
    534 posts

    Posted 23 Mar 2009 Link to this post

    Hi Mitch,

    The only scenario not fully covered by OpenAccess is when the ID of the child object depends on the one of the parent and the parent's id is an autoinc one. In that situation the scope does not know the actual id of the parent and tries to insert null into the child id field. If you have his case you should use two transactions - one for inserting the parrent and one for the child object.

    If you have any othere difficulties/sugestions with OpenAccess we are looking forward for your feedback.

    Best wishes,
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  5. Jayavidya
    Jayavidya avatar
    14 posts
    Member since:
    Oct 2010

    Posted 01 Dec 2010 Link to this post

     I tried this same parent-child 1:N insert operation. Telerik Orm supports this. I had to insert 1 parent with 4 child values as collection attached to it. I executed the following steps.
    1. App.config of parent class has the following code
    <class name="Parent">
                <extension key="db-key-generator" value="AUTOINC" />

    2. App.config of child class has the following code
    <class name="Child">
    <field name="parentId">
                  <extension key="dependent" value="true" />
                  <collection element-type="Parent" />

    3. Orm class of Parent has the following code
           private IList<Child> currencyMapPeriods = new List<Child>();

    4. Object scope addition

    It works!
Back to Top