How to get indentity column row after insert using LINQ

Can any of you show me how to complete the following tasks?

// Prepare object to be saved
// Note that MasterTable has MasterTableId as a Primary Key and it is an indentity column

MasterTable masterTable = new MasterTable();
masterTable.Column1 = "Column 1 Value";
masterTable.Column2 = 111;

// Instantiate DataContext
DataContext myDataContext = new DataContext("<<ConnectionStrin>>");

// Save the record
myDataContext.MasterTables.InsertOnSubmit(masterTable);
myDataContext.SubmitChanges();

// ?QUESTION?
// Now I need to retrieve the value of MasterTableId for the record just inserted above.

Yours faithfully

+3
source share
2 answers

An identifier value is assigned to the inserted object immediately after calling SubmitChanges.

Just access:

masterTable.MasterTableId
+5
source

, LINQ to SQL . , ( AddressID, , ID), , . SubmitChanges ?

0

Source: https://habr.com/ru/post/1705057/


All Articles