Page 1 of 1

sequence of saving parent/child records

Posted: Thu 01 Oct 2009 10:06
by sitek
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 ?

Posted: Fri 02 Oct 2009 10:19
by Zero-G.
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]

Posted: Fri 02 Oct 2009 10:21
by Zero-G.
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.