tags:

views:

107

answers:

3
var e1 = new E1();
e1.e2s.Add(new e2()); //e2s is null until e1 is saved, i want to save them all at the same time
context.e1s.imsertonsubmit(e1);
context.submitchanges();
A: 

Well - I don't know if your initial code block would work, but I'm guessing you have to mark your new e2 as insert on submit. Thus:

var e1 = new E1();
var e2 = new e2();
e1.e2s.Add(e2); //e2s is null until e1 is saved, i want to save them all at the same time
context.e1s.insertonsubmit(e1);
context.e2s.insertonsubmit(e2);
context.submitchanges();
John Christensen
A: 

there we go, apparently when you create another ctor, you have to actually call the no arg ctor in order for the stuff in the ctor to happen

Gabe Anzelini
+1  A: 

The sub items will be saved along with the main item, and even identities will be set properly, if you give your DataClasses an association between these classes.

You do this by adding LoadOptions to your O/R-Designer DataClasses like this:

  MyDataContext mydc = new MyDataContext();
  System.Data.Linq.DataLoadOptions lo = new System.Data.Linq.DataLoadOptions();
  lo.LoadWith<E1>(p => p.e2s);
  mydc.LoadOptions = lo;

This way LINQ will take care of adding the sub-items, you don't need to InsertOnSubmit every one by itself. A side effect: upon loading the item, the subitems will be retrieved, too.

Sam