简体   繁体   English

实体框架中的 Scope_Identity()

[英]Scope_Identity() in Entity Framework

What is the equal code in EntityFramework to this sql code? EntityFramework 中与此 sql 代码相同的代码是什么?

select Scope_Identity()

I want to get the id of the last record that i have inserted to database in EF.我想获取我插入到 EF 数据库中的最后一条记录的 ID。

You must ensure that your key property is mapped with StoreGeneratedPattern.Identity ( here you can find some more about this setting).您必须确保您的密钥属性与StoreGeneratedPattern.Identity映射( 在这里您可以找到有关此设置的更多信息)。 This should be used as default when generating the model from MS SQL database where the key column is defined with IDENTITY .当从 MS SQL 数据库生成模型时,这应该用作默认值,其中键列是用IDENTITY定义的。 After that it is enough to call SaveChanges on the context and the key property will be automatically filled for you.之后,在上下文上调用SaveChanges就足够了,键属性将自动为您填充

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM