I would like to retrieve the inserted ID after executing this command:
db.Database.ExecuteSqlCommand("INSERT INTO Table_Ressources (IdTypeRessource, IdSociete, IdTypeFormatIntitule_UR)Values (1,1,1);
How should I do it please?
I would like to retrieve the inserted ID after executing this command:
db.Database.ExecuteSqlCommand("INSERT INTO Table_Ressources (IdTypeRessource, IdSociete, IdTypeFormatIntitule_UR)Values (1,1,1);
How should I do it please?
You want to use OUTPUT with ExecuteScalar to return the ID of the record INSERTED (or even updated).
INSERT INTO Table_Ressources (IdTypeRessource, IdSociete, IdTypeFormatIntitule_UR)
OUTPUT INSERTED.Id
VALUES (1,1,1);
The statement will return the ID of the inserted record.
Some reference:
https://msdn.microsoft.com/en-us/library/ms177564.aspx
How do I use an INSERT statement's OUTPUT clause to get the identity value?
Try adding SELECT SCOPE_IDENTITY()
to the command, and look at the return value of ExecuteSqlCommand
You need to use the "ExecuteScalar" instead of "ExecuteSqlCommand". The ExecuteScalar does what you need as explained here: