Let’s say I have a Company table and a Division table. Each Division has a Foreign key to a company so each company can have many Division children.
If I grab a Company object with Linq-to-sql I have a access to it’s Divisions property, a entity set of Division objects. If I add a new Division object to it and call SubmitChanges() won’t it automatically go into the Division table or am I forced to call InsertOnSubmit?
There are multiple ways to insert objects into the database with LINQ to SQL. For instance:
Or:
As you can see, you can use the
InsertOnSubmitof theTable<Division> Divisionsproperty on the data context, but you can also use theAddmethod of theEntitySet<Division> Divisionsproperty on theCompanyentity. They both do -about- the same. Nice about the latter approach is that you don’t need to ‘link’ the company to the new division, because LINQ to SQL can figure that out for you.I hope this answers your question.