实体框架中的Scope_Identity() [英] Scope_Identity() in Entity Framework

查看:83
本文介绍了实体框架中的Scope_Identity()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EntityFramework中与此SQL代码相等的代码是什么?

select Scope_Identity()

我想获取我插入到EF数据库中的最后一条记录的ID.

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

推荐答案

您必须确保键属性已通过 StoreGeneratedPattern.Identity (关键属性将自动为您填充.

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.

这篇关于实体框架中的Scope_Identity()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆