I have the following LINQ to SQL insert code:
public static int InsertFileToQueue(FileInfo file)
{
int? recordID = null;
IpsDBDataContext db = new IpsDBDataContext();
IpsJobFileQueue record = new IpsJobFileQueue();
record.FileName = file.Name;
record.FilePath = file.FullName;
record.PickupDate = file.CreationTime;
record.StartTime = null;
record.EndTime = null;
record.ProcessCode = null;
db.SubmitChanges();
return recordID;
}
Somewhere after the db.SubmitChanges() I know I need to add some code to retrieve the id on the record I just inserted. The things I can’t rely on are the filename (as many of these files will be named the same) and certainly not any of the times.
So what do I query for to get the ID?
First you should call the Table<TEntity>.InsertOnSubmit(TEntity) method passing the
IpsJobFileQueueinstance to persist. Then, after the DataContext.SubmitChanges() method has completed, you can retrieve the assigned primary key value from the corresponding property on theIpsJobFileQueueobject.In this example
IpsJobFileQueue.Idis the property that has been mapped to the primary key column of the corresponding database table.