How can I retrieve the primary key of a newly created record by LINQ2SQL?
var peon = new Peon { name = 'Serf' };
context.Peons.InsertOnSubmit(peon);
context.SubmitChanges();
Will the LINQ2SQL model automagically get updated with the primary key? I wasn’t able to find any information on this in the docs.
Or is there some other way to get the primary key of the last inserted record? Preferably without searching for the record.
The context of my question is that my class is associated to another class by many-to-many table. I need the primary key to insert the association to that table. For example:
// After previous code.
var bossPeonMapping = new { PeonId = peon.Id, BossId = boss.Id };
// Insert mapping
I’d appreciate any tips if LINQ2SQL affords me a better way to do this.
After you submit changes the entity should have it’s primary key value.