从 DataServiceContext 查询
我有一个 oData 生成的 DataServiceContext,并且我已成功向其中添加实体。我需要添加一整堆实体,然后将它们提交到单个 SaveChanges 中,并在最后设置 Batch 选项。这一切都很好,直到我在保存更改之前查询它。
概要是:
- 创建一个新实体
- 将其添加到 DataServiceContext
- 在上下文中运行查询,查找我刚刚添加的项目 - 未找到
我之前使用 EF4 的工作表明,如果这是一个实体上下文,一切都会好起来的,但因为这是一个服务上下文,我无法查询已添加但未保存到服务中的实体。
是这样吗?
I have an oData generated DataServiceContext and I am successfully adding entities to it. I need to add a whole load of entities and then commit them in a single SaveChanges with the Batch option set at the end. This is all fine, until I come to query it before the save changes.
Outline is:
- Create a new entity
- Add it to the DataServiceContext
- Run a query on the context looking for the item I have just added - IT IS NOT FOUND
My previous work with EF4 would suggest that if this was an Entity Context, all would be fine, but because this is a Service Context I cannot query for an entity that has been added but not saved to the service.
Is this the case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DataServiceContext 基本上只是一个小帮手。对它运行任何查询都会直接在服务器上运行查询,客户端不会尝试以任何方式修复数据。由于您的更改尚未到达服务器(尚未调用 SaveChanges),因此查询将不会返回新添加的实体。
如果您确实需要列出在 SaveChanges 之前添加的实体,您可以使用 DataServiceContext.Entities 集合,该集合将为上下文跟踪的所有实体返回 EntityDescriptor。您可以通过查找状态为“已添加”的内容来列出已添加的内容。
DataServiceContext is basically just a small helper. Running any query against it will run the query on the server directly, the client will not try to fixup the data in any way. Since you're changes haven't made it to the server yet (SaveChanges was not called yet), the query will not return the newly added entities.
If you really need to list the entities you've added before SaveChanges, you could use the DataServiceContext.Entities collection which will return EntityDescriptor for all entities tracked by the context. You can list those added by looking for those with state Added.