是查询新添加的对象可以在MS实体框架 [英] Is query to new added object possible in MS Entity Framework

查看:109
本文介绍了是查询新添加的对象可以在MS实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来查询或刚刚接触新添加的对象(使用ObjectContext.AddObject方法)的实体框架?我的意思的情况时尚未保存到数据存储使用的SaveChanges

Is there a way to query or just access newly added object (using ObjectContext.AddObject method) in Entity Framework? I mean situation when it is not yet saved to data store using SaveChanges

据我所知,查询被转换为基础SQL以及对数据存储执行,并没有这个新对象呢。但无论如何,我很好奇 - 如果它不oficially支持,也许有可能在理论上。如果不是,如何开发人员可以处理它?手动跟踪新对象,并使用LINQ to Objects查询呢?

I understand that queries are translated to underlying SQL and executed against data store, and it don't have this new object yet. But anyway, I'm curious - if it is not oficially supported, maybe it is possible in theory. If it's not, how developer can deal with it? Manually track new objects and query them using Linq to objects?

同样的问题也适用于LinqToSql。

The same question also applies to LinqToSql.

推荐答案

在EF,如果你使用这个code,你把所有的都在范围内(包括新增加的)已加载的实体:

In EF, if you use this code, you have all the entities that are already loaded in the context (including newly added ones) :

context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Unchanged).Select(o => o.Entity).OfType<YourObjectType>()

这篇关于是查询新添加的对象可以在MS实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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