清除本地集合而不处理上下文

编程入门 行业动态 更新时间:2024-10-09 04:23:41
本文介绍了清除本地集合而不处理上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I'm using EF6 and VB 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.

更多推荐

清除本地集合而不处理上下文

本文发布于:2023-11-09 07:59:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571808.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   而不

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!