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)
精彩评论