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

[Solved] Insert record in child table (1:N Relationship)

8 Answers 342 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.
Ratndeep
Top achievements
Rank 1
Ratndeep asked on 30 Dec 2009, 01:57 PM
Hi,
I know this is a very basic question should not be asked but somehow stuck because of this. I have one parent table and a child table which is referring parent table and there is 1:N relationship between parent and child. How to save child with parent table as its using parent table primary key.

Thanks in advance.

8 Answers, 1 is accepted

Sort by
0
devoas
Top achievements
Rank 1
answered on 30 Dec 2009, 06:57 PM
Hi Ratndeep,

I am trying to answer your question.

In case you are using Reverse mapping and Primary / Child objects have managed Collection (which you can set from Forward Maping -> Persistent -> Object - > Relation  ------>  [x] Manage Collection) then you can simply add child in Parent Object. Following is the code assuming Order (parent) and OrderLine (Child) having  OrderId as relation field.

Order order = new Order();   //Parent Object : having autonic field OrderID 
order.CustomerID = 2
 
 
OrderLine orderline = new OrderLine();  // Child Object  
orderline.ProductID = 2
 
order.OrderLine.Add(orderline);  // Adding Child Object in Parent 
 
scope.Transaction.Begin(); 
scope.Add(order); 
scope.Transaction.Commit(); 
 
// Child object will be saved automatically and OrderId will be transfered in Child Object 

Hope above code may help you...

Thanks,
devoas

0
Damyan Bogoev
Telerik team
answered on 04 Jan 2010, 09:01 AM
Hello Ratndeep,

You could use the Robin Devoas’s solution to insert a new parent table and a new child table records using the parent table’s primary key.  In case of modifying an existing parent table record by adding a new child table record you should do the following:
1. Follow the instructions from the Robin Devoas’s answer to make the collection managed
2.
Order order = scope.Extent<Order>().Where(/*filter*/);
    
OrderLine orderline = new OrderLine(); 
orderline.ProductID = 2; 
...
scope.Transaction.Begin();  
order.OrderLine.Add(orderline); 
scope.Transaction.Commit();

I hope that will help you.

Best wishes,
Damyan Bogoev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
test
Top achievements
Rank 1
answered on 14 Nov 2010, 01:49 PM
Sorry I reup this post because I have a problem with Q3 2010 in this scenario.

With version Q2 714, no problem to add a child record to my parents, since Q2 9... and Q3 it seems OrderId is not inserted in OrderLine Table because I've a constraint error.

Here's my code :

Dim oDbContext As New TestContext
Dim oOrder As New Order
oOrder.CustomerId = 2
 
Dim oOrderLine As New OrderLine
oOrderLine.ProductId = 2
 
oOrder.OrderLines.Add(oOrderLine)
 
oDbContext.Add(oOrder)
oDbContext.SaveChanges()

And the error :

Insert of '1795977256-0' failed: Telerik.OpenAccess.RT.sql.SQLException: L'instruction INSERT est en conflit avec la contrainte FOREIGN KEY "ref_OrderLines_Orders".

What's wrong with my code ? This code was working with Q2 714.

Thanks for your help.
0
Zoran
Telerik team
answered on 15 Nov 2010, 06:24 PM
Hi test,

 It seems that the collection that you are inserting into is not managed. You could change that with the following steps:

  • Select the Order class in the diagram of the Visual Designer.
  • In the "Mapping Details Editor" select the "Associations" tab. 
  • In the Associations tab of the Mapping Details Editor select the association in question (the one between Orders and OrderLines).
  • Check the "Managed" check-box in the editor.

Sincerely yours,
Zoran
the Telerik team
See What's New in Telerik OpenAccess ORM in Q3 2010!
Monday, November 15, 11 am Eastern Time: Register here>>
Monday, November 15, 10 pm Eastern Time: Register here>>
0
test
Top achievements
Rank 1
answered on 15 Nov 2010, 08:14 PM
Thanks Zoran,

Something changed but it's not ok.

When I checked the "Managed" check-box and run the application I've got an error on the code :

oDbContext.Add(oOrder)

This is the error :

No row for Test.Order ('Orders') GenericOID@1 Order OrderId=0

What's wrong ?
0
Serge
Telerik team
answered on 16 Nov 2010, 06:27 PM
Hello test,

 Usually this means that there is some mismatch in the IDs. Can you please check that there is no record in the database that has its OrderID set to 0, also make sure that the OrderID of the class that has an Order reference is nullable, because if not, the default value for int is 0 and OpenAccess will try and retrieve the corresponding Order object that has an id with the value of 0 which is invalid.

You could also try setting the Return Null on RowNotFound checkbox to true in the Backend Setting tab of the Model Settings dialog (you can open it by right clicking on the diagram and selecting Show Model Settings).

If however the issue still persists you could send us the project you are working on, or rather a smaller one just showing this behaviour that we can use to debug locally and quickly find the problem.

I am looking forward to resolving this problem with you. 

Sincerely yours,
Serge
the Telerik team
See What's New in Telerik OpenAccess ORM in Q3 2010!
Monday, November 15, 11 am Eastern Time: Register here>>
Monday, November 15, 10 pm Eastern Time: Register here>>
0
test
Top achievements
Rank 1
answered on 16 Nov 2010, 07:32 PM
Thanks Serge, it's great, it's working.

I've tried the 2 solutions, the 2 works.

I think I will choose the second solution because I have a lot of classes with this problem. Do you know if the Return Null on RowNotFound checkbox is a new feature since Q2 714 version ? Because all was ok in this previous version.


0
Serge
Telerik team
answered on 17 Nov 2010, 06:04 PM
Hi test,

 What we have changed since Q2 is that by default the navigational collection members have the property managed set to false. Previously this was always set to true and was not even public, however we have found out that this led to certain overheads and this is why we decided to make it optional. 

If you have created new tables and associations between them this might have been the cause. If however this is not the case we would really appreciate if you can send us your project, prior to making the previous changes so that we can debug and investigate if there has been some regression.

I hope this is helpful.

Best wishes,
Serge
the Telerik team
See What's New in Telerik OpenAccess ORM in Q3 2010!
Monday, November 15, 11 am Eastern Time: Register here>>
Monday, November 15, 10 pm Eastern Time: Register here>>
Tags
General Discussions
Asked by
Ratndeep
Top achievements
Rank 1
Answers by
devoas
Top achievements
Rank 1
Damyan Bogoev
Telerik team
test
Top achievements
Rank 1
Zoran
Telerik team
Serge
Telerik team
Share this question
or