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

Scope Identity

4 Answers 160 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.
Kareem
Top achievements
Rank 1
Kareem asked on 05 Mar 2009, 08:58 PM
Hello,

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?

Example:

  Try

      scope.Transaction.Begin()

      
       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

    

     scope.Add(parent)
     scope.Add(child)

     scope.Transaction.Commit()

    Catch e As Exception
      scope.Transaction.Rollback()
    Finally

    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?

Thanks,
Kareem

4 Answers, 1 is accepted

Sort by
0
Zoran
Telerik team
answered on 07 Mar 2009, 04:47 PM
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,
Zoran
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.
0
SuperXRAY
Top achievements
Rank 2
answered on 18 Mar 2009, 07:21 AM
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.
0
Zoran
Telerik team
answered on 23 Mar 2009, 03:51 PM
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,
Zoran
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Jayavidya
Top achievements
Rank 1
answered on 01 Dec 2010, 01:06 PM
Hello,
 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
[Telerik.OpenAccess.Depend()]
       private IList<Child> currencyMapPeriods = new List<Child>();

4. Object scope addition
ObjectScope.Transaction.Begin();
ObjectScope.Add(Parent);
ObjectScope.Transaction.Commit();

It works!
Tags
General Discussions
Asked by
Kareem
Top achievements
Rank 1
Answers by
Zoran
Telerik team
SuperXRAY
Top achievements
Rank 2
Jayavidya
Top achievements
Rank 1
Share this question
or