sequence of saving parent/child records

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
sitek
Posts: 3
Joined: Thu 01 Oct 2009 09:35

sequence of saving parent/child records

Post by sitek » Thu 01 Oct 2009 10:06

I need to create first child record and then parent record.

Code: Select all

            Testdatacontext dc = new Testdatacontext();
            Testchild tc = new Testchild();
            tc.Text = "one";
            dc.Testchilds.InsertOnSubmit(tc);
            ...
            ...
            Test t = new Test();
            t.Text = "numbers";
            dc.Tests.InsertOnSubmit(t);
            tc.Test = t;
            ...
            dc.SubmitChanges();
SubmitChanges() generates the exception "Error on executing DbCommand.", because the insert command for child records is generated first.
Can LINQ generate insert commands in right sequence (depending on the relations), or is there any way to do this manually ?

Zero-G.
Posts: 398
Joined: Sat 09 Dec 2006 12:20

Post by Zero-G. » Fri 02 Oct 2009 10:19

Hey

This, what you try, can not be supported by LinQ
Even, if you have a foreign_key_relation betwenn the master & child.

So, what you can do is the following:

Code: Select all

            Testdatacontext dc = new Testdatacontext();
            Testchild tc = new Testchild();
            tc.Text = "one";
            ... 
            ...
            Test t = new Test();
            t.Text = "numbers";
            t.testchild.add(tc)
            dc.Tests.InsertOnSubmit(t);
            ...
            dc.SubmitChanges();
Hope this helps.

[/code]

Zero-G.
Posts: 398
Joined: Sat 09 Dec 2006 12:20

Post by Zero-G. » Fri 02 Oct 2009 10:21

Hey

This, what you try, can not be supported by LinQ
Even, if you have a foreign_key_relation betwenn the master & child.

So, what you can do is the following:

Code: Select all

            Testdatacontext dc = new Testdatacontext();
            Testchild tc = new Testchild();
            tc.Text = "one";
            ... 
            ...
            Test t = new Test();
            t.Text = "numbers";
            t.testchild.add(tc)
            dc.Tests.InsertOnSubmit(t);
            ...
            dc.SubmitChanges();
Hope this helps.

Post Reply