EF4 更新表中所有行的值而不执行选择
在运行更新之前,我需要重置特定表中的布尔字段。 该表可能有 100 万条左右的记录,我不希望在更新之前进行选择,因为它花费了太多时间。
基本上我在代码中需要的是在 TSQL 中生成以下内容
update tablename
set flag = false
where flag = true
我有一些接近我需要的东西 http://www.aneyfamily.com/terryandann/post/2008/04/Batch-Updates-and-Deletes-with-LINQ- to-SQL.aspx 但尚未实施,但想知道是否有更标准的方法。
为了保持在这个项目的限制范围内,我们不能使用存储过程或直接在上下文的 ExecuteStoreCommand 参数中编写 TSQL,我相信您可以做到。
我知道我需要做的事情可能不会在 EF4 中直接支持,我们可能需要查看 SPROC 来完成这项工作[在完全没有任何其他方式的情况下],但我只需要首先充分探索所有可能性。 在 EF 理想世界中,上面更新标志的调用是可能的,或者也可以获取具有 id 和布尔标志的实体,仅减去关联的实体,然后循环实体并设置标志并执行单个操作SaveChanges 调用,但这可能不是它的工作方式。
任何想法,
提前致谢。 利亚姆
I need to reset a boolean field in a specific table before I run an update.
The table could have 1 million or so records and I'd prefer not to have to have to do a select before update as its taking too much time.
Basically what I need in code is to produce the following in TSQL
update tablename
set flag = false
where flag = true
I have some thing close to what I need here http://www.aneyfamily.com/terryandann/post/2008/04/Batch-Updates-and-Deletes-with-LINQ-to-SQL.aspx
but have yet to implement it but was wondering if there is a more standard way.
To keep within the restrictions we have for this project, we cant use SPROCs or directly write TSQL in an ExecuteStoreCommand parameter on the context which I believe you can do.
I'm aware that what I need to do may not be directly supported in EF4 and we may need to look at a SPROC for the job [in the total absence of any other way] but I just need to explore fully all possibilities first.
In an EF ideal world the call above to update the flag would be possible or alternatively it would be possible to get the entity with the id and the boolean flag only minus the associated entities and loop through the entity and set the flag and do a single SaveChanges call, but that may not be the way it works.
Any ideas,
Thanks in advance.
Liam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会去找利益相关者,他介绍了不直接使用 SQL 或 SPproc 的限制,并向他展示以下事实:
这一切都是为了使用正确的工具来满足正确的要求。
顺便说一句。可以避免加载相关表。这只是关于您运行的查询。不要使用 Include 并且不要访问导航属性(在延迟加载的情况下),并且您将不会加载关系。
可以仅选择 Id(通过投影)、创建虚拟实体(仅将 id 和标志设置为 true)并仅执行标志的更新,但它仍将执行最多 1M 的更新。
此外,它只能在空对象上下文中工作(或者至少更新表中的实体不能附加到上下文)。因此,在某些情况下,在其他更新之前运行此操作将需要两个
ObjectContext
实例 = 手动共享DbConnection
或两个数据库连接,并且在事务 = 分布式事务和另一个性能影响的情况下。I would go to stakeholder who introduced restirctions about not using SQL or SProc directly and present him these facts:
It is all about using the right tool for the right requirement.
Btw. loading of realted tables can be avoided. It is just about the query you run. Do not use Include and do not access navigation properties (in case of lazy loading) and you will not load relation.
It is possible to select only Id (via projection), create dummy entity (set only id and and flag to true) and execute only updates of flag but it will still execute up to 1M updates.
Moreover it will only work in empty object context (or at least no entity from updated table can be attached to context). So in some scenarios running this before other updates will require two
ObjectContext
instances = manually sharingDbConnection
or two database connections and in case of transactions = distributed transaction and another performance hit.创建一个新的 EF 模型,并且仅添加您需要进行更新的一个表。这样,所有的连接就不会发生。这将大大加快您的处理速度。
Make a new EF model, and only add the one Table you need to make the update on. This way, all of the joins don't occur. This will greatly speed up your processing.
http://msdn.microsoft.com/en -us/library/system.data.objects.objectcontext.executestorecommand.aspx
编辑
抱歉,没有把帖子看完。
http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.executestorecommand.aspx
Edit
Sorry, did not read the post all the way.