清除本地集合而不处理上下文 [英] Clear a local collection without disposing the context

查看:82
本文介绍了清除本地集合而不处理上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF6和VB.net 2017.我有这个代码:

I'm using EF6 and VB.net 2017. I have this code :

context.Entry(Myobj1).Collection("Child_Itm").Load()
bindingsource1.datasource=context.Child_itm.Local.Tobindinglist


问题是,在某些时候我想加载另一个对象的子集合如下:

The problem is that , after that at some point i want to load the child collection for another object like this :

context.Entry(Myobj2).Collection("Child_Itm").Load()
bindingsource1.datasource=context.Child_itm.Local.Tobindinglist


之后,在我的bindingsource上,所有加载的项目都是第一次和第二次加载调用。如何在加载新数据之前清除本地集合?在调用第二次加载之前,我尝试过:

But after that , on my bindingsource the are all the items loaded from the first and the second call of load. How can i do to clear the local collection before loading new data ? Before calling the second load , I've tried with :

context.child_itm.local.clear 


,但对象仍然存在于本地。我想删除所有项目,甚至不保持分离状态但是我想在不处理上下文的情况下这样做。

, but the objects still exist on local. I want to remove all items , and not keeping even on Detached status But i want to do that without disposing the context.

有什么办法吗?

推荐答案

您应该销毁上下文或在上下文中创建一个新集合。

You should destroy the context or make a new collection within the context.

这里是其他要考虑的事项,如果你没有销毁上下文并且你在仍然连接到上下文的集合上循环迭代,那么EF会返回并从数据库中再次读取每个对象。你创建100,000个对象$ b查询中的$ b。然后循环遍历集合,再次为集合中的每个对象读取100,000个相同数据。集合应该是断开连接的集合。

And here is something  else to consider, if you don't destroy the context and you iterate in a loop over the collection that is still connected to the context, then EF goes back and reads each oject again from the database. You create 100,000 objects in the query. You then loop over the collection and that is 100,000 reads again of the same data for each object in the collection. The collection should be a disconnected collection.


这篇关于清除本地集合而不处理上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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