Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 243
Neil Barnwell Avatar asked Jan 23 '23 19:01

Neil Barnwell


1 Answers

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!

like image 121
leppie Avatar answered Jan 29 '23 10:01

leppie