views:

638

answers:

1

In LINQ to SQL, is InsertOnSubmit() required when adding via a Foreign Key?

I.e. If I have code that does the following, where orders and order lines are linked in the dbml and database via a foreigh key:

  • Create order.
  • Add order to datacontext orders.
  • Create order line.
  • Add to order.Lines.

Do I still need to add the order line to the context.OrderLine collection?

+6  A: 

No, in almost all cases it is not needed if you attach (assigned to property or add to collection of entity) it to an attached object (on that was pulled from the DB). If the root object is disconnected, but all other data relates to it, you only need to insert the root object :)

Kinda cool, hey!

leppie