After creating a new object (Foo), I set the key (BarId) for the association property EntityRef (Bar). Then I want to insert a new object into the database and after that access the lazy loaded child.
Unfortunately, the lazy-load property returns null after calling InsertOnSubmit (). It returns the correct object if I instead bind () the object to a data context.
Here is the code that successfully inserts my new object into the database, but incorrectly configured lazy loading for the property of the child bar:
var foo = new Foo();
foo.BarId = 123;
context.GetTable<Foo> ().InsertOnSubmit( foo );
foo.Bar.Something();
Here the Bar object is loaded correctly:
var foo = new Foo();
foo.BarId = 123;
context.GetTable<Foo> ().Attach( foo );
foo.Bar.Something(); // method is called on lazy-loaded Bar object
The Attach call before InsertOnSubmit causes the latter to throw an exception "Cannot add an entity that already exists."
, LINQ-to-SQL, ?
?