Handling auto-incrementing IDENTITY SQL Server fields with LINQ to SQL in C#
- by Maxim Z.
I'm building an ASP.NET MVC site that uses LINQ to SQL to connect to SQL Server, where I have a table that has an IDENTITY bigint primary key column that represents an ID.
In one of my code methods, I need to create an object of that table to get its ID, which I will place into another object based on another table (FK-to-PK relationship).
At what point is the IDENTITY column value generated and how can I obtain it from my code?
Is the right approach to:
Create the object that has the IDENTITY column
Do an InsertOnSubmit() and SubmitChanges() to submit the object to the database table
Get the value of the ID property of the object