开发者

How to get last created ID from SQL database using Linq-to-SQL / Linq-to-Entities (MVC)

开发者 https://www.devze.com 2023-03-20 01:55 出处:网络
I want to get the la开发者_StackOverflow社区st entry into the database, I usually used SCOPE_IDENTITY() in ado.net to get the last created id, but how to achieve that in Linq-to-SQL or Linq-to-Entitie

I want to get the la开发者_StackOverflow社区st entry into the database, I usually used SCOPE_IDENTITY() in ado.net to get the last created id, but how to achieve that in Linq-to-SQL or Linq-to-Entities?

For instance I need to get the last added bookingID from the booking table whose userID=xxxx.


Both linq-to-sql and EF do this automatically. You just need to insert an object, save/submit changes and check id property in your object. If correctly configured it should be filled with scope identity.

  • In case of EF the property must be configured with StoreGeneratedPattern.Identity
  • In case of Linq-to-Sql the property must be configured with IsDbGenerated = true (Auto Generated Value in dbml designer)
0

精彩评论

暂无评论...
验证码 换一张
取 消