LINQ to Entities-调用SaveChanges()后如何最好地获取IDENTITY值 [英] LINQ to Entities - How best to obtain the IDENTITY value after calling SaveChanges()

查看:117
本文介绍了LINQ to Entities-调用SaveChanges()后如何最好地获取IDENTITY值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在执行插入操作后,在此站点上存在许多与检索身份有关的问题.我们获取身份的方法是在调用SaveChanges()之后立即进行下面的调用.

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();

这似乎始终如一地工作可能已经足够了;但是,如果在调用之间添加另一个记录,则可能会出现错误.因此,第一个问题是,考虑到EF在跨跨上下文的情况下执行,这种方法听起来合理吗?

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-如何在InsertOnSubmit()之后查找IDENTITY列的值

在该答案中,调用SubmitChanges()之后,以下调用(其中"tst"代表用户的类)检索该值.

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)

这似乎在LINQ to Entities中的工作原理完全相同,在调用保存更改之后,该类的实例现在包含id.

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;

由于我们要询问类实例的实际ID(实际记录),因此它似乎是故障安全的.而且,EF的设计者应该提供这样的基本功能似乎是合乎逻辑的.任何人都可以确认这是获得身份的正确方法或至少是最佳实践吗?

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?

推荐答案

是的,在SaveChanges为叫.对于任何无法提前设置的外键,它也会这样做(例如,将新的父行+新的子行保存在一起,并且在SaveChanges之后,您将在子行的FK中拥有正确的值)值).

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:

4-如果INSERT操作成功,则服务器生成的值将写回到ObjectStateEntry.

4 - If the INSERT operation succeeds, server-generated values are written back to the ObjectStateEntry.

5-ObjectStateEntry使用服务器生成的值更新对象.

5 - The ObjectStateEntry updates the object with the server-generated value.

这篇关于LINQ to Entities-调用SaveChanges()后如何最好地获取IDENTITY值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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