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
| 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 |
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.
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 TestContextDim oOrder As New OrderoOrder.CustomerId = 2Dim oOrderLine As New OrderLineoOrderLine.ProductId = 2oOrder.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.
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.
Zoran
the Telerik team
Monday, November 15, 11 am Eastern Time: Register here>>
Monday, November 15, 10 pm Eastern Time: Register here>>
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=0What's wrong ?
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.
Serge
the Telerik team
Monday, November 15, 11 am Eastern Time: Register here>>
Monday, November 15, 10 pm Eastern Time: Register here>>
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.
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.
Serge
the Telerik team
Monday, November 15, 11 am Eastern Time: Register here>>
Monday, November 15, 10 pm Eastern Time: Register here>>