Scope_Identity() in Entity Framework

泪湿孤枕 提交于 2021-02-07 09:44:55

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/5646163/scope-identity-in-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!