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

Foreign key is null error when inserting new nested objects

3 Answers 105 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.
Myth
Top achievements
Rank 1
Myth asked on 29 Jan 2010, 08:32 AM
I been looking through the documentation andnorthwind example, but can't seem to find an answer. I used both the HIGHLOW method and AUTOINC without success.

I have a form in which i have some textfields and a gridview. The textfields represent data for an order, and the gridview rows represent OrderLines (details about what products are being bought).

When the user clicks save, i'm creating a new order object, and add new orderline objects to a list in the order object, based on the values the user entered. After all objects are created and nested, i try to add the root order object to the database. When the transaction is committed i get an error saying the order_id field for the orderlines is null. What do i need to do in order to get it to be the id of the root object?

Here's my reverse mapping:

 

<table name="'OrderLines'" type="1" locked="False">  
                <column name="id" isPk="True" isAutoInc="False" adoType="4" sqlType="int" length="0" isNull="False" /> 
                <column name="order_id" isPk="False" isAutoInc="False" adoType="4" sqlType="int" length="0" isNull="False" /> 
                <column name="product_id" isPk="False" isAutoInc="False" adoType="4" sqlType="int" length="0" isNull="True" /> 
                <column name="unit_count" isPk="False" isAutoInc="False" adoType="4" sqlType="int" length="0" isNull="True" /> 
                <column name="unit_price" isPk="False" isAutoInc="False" adoType="3" sqlType="decimal" length="18" scale="4" isNull="True" /> 
                <constraint name="FK_OrderLines_Orders" table="'OrderLines'" dest="'Orders'">  
                    <column name="order_id" /> 
                </constraint> 
                <constraint name="FK_OrderLines_Products" table="'OrderLines'" dest="'Products'">  
                    <column name="product_id" /> 
                </constraint> 
                <class name="OrderLine" namespace="TelerikUI.Domain" check="-1" generated="True">  
                    <field name="_id" type="1" isPK="True" column="id" valueType="System.Int32" /> 
                    <field name="_orderId" type="1" isPK="False" column="order_id" valueType="System.Int32" /> 
                    <field name="_productId" type="1" isPK="False" column="product_id" valueType="System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" /> 
                    <field name="_unitCount" type="1" isPK="False" column="unit_count" valueType="System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" /> 
                    <field name="_unitPrice" type="1" isPK="False" column="unit_price" valueType="System.Nullable`1[[System.Decimal, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" /> 
                    <field name="_order" type="2" isPK="False" reference="'Orders'">  
                        <mapping field="Order._id" column="order_id" /> 
                        <inverse name="_orderLines" /> 
                    </field> 
                    <field name="_product" type="2" isPK="False" reference="'Products'">  
                        <mapping field="Product._id" column="product_id" /> 
                    </field> 
                </class> 
            </table> 

So currently i'm trying to use the highlow key generator, but having auto increment didn't help.

 



Any ideas or basic tutorials on how to insert nested data? I couldn't find one. The documentation is a bit badly organized.

 

3 Answers, 1 is accepted

Sort by
0
Myth
Top achievements
Rank 1
answered on 29 Jan 2010, 08:39 AM
Ugh, why do i always figure out the answer after i make a thread.

Problem was that i just added the orderlines to the list inside the order object, without also setting the orderline.order variable to the order object.
0
Accepted
Alexander
Telerik team
answered on 29 Jan 2010, 09:16 AM
Hi Myth,

You can actually let OpenAccess do this for you. Just enable the "Managed collection" option from the Forward mapping wizard while the orderlines collection is selected in the treeview. Afterwards you will not need to set manually the orderline.order reference. Hope that helps.

Best wishes,
Alexander
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
Myth
Top achievements
Rank 1
answered on 29 Jan 2010, 09:28 AM
Thanks for the info!

I didn't know you could do settings in both forward and reverse mapping. I thought you just had to use 1 or the other.
Tags
General Discussions
Asked by
Myth
Top achievements
Rank 1
Answers by
Myth
Top achievements
Rank 1
Alexander
Telerik team
Share this question
or