In MySQL, you can get the last inserted field identifier with auto increment using
MySQL only
SELECT last_insert_id() as Last_ID
This will work provided . Entity Framework uses SQL to manage a MySQL database.
A simple test will answer what I think.
Note that you will only get the last inserted identifier from the current connection (usually this is what you want).
There is no way (I know) to get the last inserted id of other connections except by doing
SELECT MAX(id) FROM sometable as Last_ID
What will work if the insert from another connection has been completed.
Make it portable to other DBs
If you want to make this solution portable to another database, you can make the saved function as follows:
CREATE FUNCTION GetLastID: integer; BEGIN DECLARE lastid: integer; SELECT last_insert_id() INTO lastid; RETURN lastid; END;
You can then modify this stored procedure if you modify the DB-backend.
No need to rewrite any other code.
SELECT GetLastID () as Last_ID
Johan source share