LINQ to Entities - 如何在调用 SaveChanges() 后最好地获取 IDENTITY 值
该网站上提出了许多与执行插入后检索 IDENTITY 相关的问题。我们获取身份的方法是在调用 SaveChanges() 之后立即进行以下调用;
context.MyClass.OrderByDescending(c => c.Id).FirstOrDefault();
这看起来始终如一地工作可能是完全足够的;但是,如果在调用之间添加另一条记录,则可能会出现错误。所以第一个问题是,考虑到 EF 在事务上下文中执行,这种方法是否合理?
其次,对以下问题的回答表明可能有更好的方法。
Linq to SQL - 如何在 InsertOnSubmit() 之后查找 IDENTITY 列的值
在该答案中,在调用 SubmitChanges() 之后,进行以下调用(其中“tst”代表用户的类别)检索值。
Response.Write("id:" + tst.id.ToString)
这似乎与 LINQ to Entities 中的工作方式完全相同,在调用 save 更改后,类的实例现在包含 id。
context.MyClass.Add(myClass);
context.SaveChanges();
int myNewIdentity = myClass.Id;
由于我们要求类实例的实际 ID(实际记录),因此它看起来是安全的。而且,EF 的设计者应该提供此类基本功能,这似乎是合乎逻辑的。任何人都可以确认这是获取身份的正确方法或至少是最佳实践吗?
There have been numerous questions posed on this site relating to the retrieval of the IDENTITY after an insert is performed. The way we have been getting the identity is to make the call below, immediately after calling SaveChanges();
context.MyClass.OrderByDescending(c => c.Id).FirstOrDefault();
This seems to work consistently may be completely adequate; however, it has the appearence of opening up a potential for error, should another record be added in between the calls. So the first question is, given that EF performs withing a transacional context, is this method sound?
Secondly, the answer provided to the following question suggests there may be a better way.
Linq to SQL - How to find the the value of the IDENTITY column after InsertOnSubmit()
In that answer, after calling SubmitChanges(), the following call (where "tst" represents the user's class) retrieves the value.
Response.Write("id:" + tst.id.ToString)
This appears to work exactly the same way in LINQ to Entities, where after the call to save changes the instance of the class now includes the id.
context.MyClass.Add(myClass);
context.SaveChanges();
int myNewIdentity = myClass.Id;
Since we are asking for the the actual ID of the class instance (actual record) it would appear to be failsafe. And, it seems logical that the designers of EF should make such basic functionality available. Can anyone confirm that this is proper way to get the identity or at least a best practice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,LINQ-to-Entities(以及就此而言的 LINQ-to-SQL)将在调用 SaveChanges 后将生成的标识列设置回实体中。对于无法提前设置的任何外键,它也会这样做(例如,一个新的父行+一个新的子行保存在一起,并且在 SaveChanges 之后,您将在子行的 FK 中拥有正确的值价值)。
您特别关心的问题记录在“使用实体密钥”页面中:
http:// msdn.microsoft.com/en-us/library/dd283139.aspx
特定部分是“实体键和添加的对象”,特定步骤是:
Yes, LINQ-to-Entities (and LINQ-to-SQL for that matter) will set the generated identity column back in the entity for you after SaveChanges is called. It will also do so for any foreign keys that couldn't be set ahead of time (for instance, a new parent row + a new child row are saved together, and after SaveChanges you'll have the right value in the child row's FK value).
Your particular concern is documented in the 'Working with Entity Keys' page:
http://msdn.microsoft.com/en-us/library/dd283139.aspx
The particular section is 'Entity Keys and Added Objects' and the particular steps are: