From what I gather, Linq to SQL doesn’t actually execute any database commands (including the opening of database connections) until the SubmitChanges() method is called. If this is the case, I would like to increase the efficiency of a few methods. Is it possible for me to retrieve the ID of an object before inserting it? I’d rather not call SubmitChanges() twice, if it’s possible for me to know the value of the ID before it’s actually inserted into the database. From a logical point of view, it would only makes sense to have to open a connection to the database in order to find out the value, but does an insertion procedure also have to take place?
Thanks
The usual technique to solve this, is to generate a unique identifier in the application layer (such as a GUID) and use this as the
ID. That way you do not have to retrieve the ID on a subsequent call.Of course, using a GUID as a primary key can have it’s drawbacks. If you decide to go this way look up COMB GUID.