在SaveChanges之前查询AddObject之后的对象? [英] Querying objects after AddObject before SaveChanges?

查看:143
本文介绍了在SaveChanges之前查询AddObject之后的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在EntityFramework中,是否可以使用AddObject查询刚被添加到上下文中的对象,然后调用SaveChanges方法?

In EntityFramework, is that possible to query the objects that have just been added to the context using AddObject but before calling the SaveChanges method?

谢谢

推荐答案

可以查询这样的对象

context.ObjectStateManager.GetObjectStateEntries(EntityState.Added).Select(obj => obj.Entity).OfType<TheEntityType>()

这将查询处于添加状态的对象。如果你想要其他的州,你可以把所有其他的州通过 GetObjectStateEntries 这样的方法。

this will query the objects which are in added state. If you want other states too you can pass all other states to GetObjectStateEntries method like this.

GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Unchanged)

这篇关于在SaveChanges之前查询AddObject之后的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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