Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope_Identity() in Entity Framework

What is the equal code in EntityFramework to this sql code?

select Scope_Identity()

I want to get the id of the last record that i have inserted to database in EF.

like image 751
WishToBePro Avatar asked Nov 27 '25 09:11

WishToBePro


1 Answers

You must ensure that your key property is mapped with StoreGeneratedPattern.Identity (here you can find some more about this setting). This should be used as default when generating the model from MS SQL database where the key column is defined with IDENTITY. After that it is enough to call SaveChanges on the context and the key property will be automatically filled for you.

like image 143
Ladislav Mrnka Avatar answered Nov 28 '25 21:11

Ladislav Mrnka