什么逻辑决定了Entity Framework 6的插入顺序

What logic determines the insert order of Entity Framework 6(什么逻辑决定了Entity Framework 6的插入顺序)
本文介绍了什么逻辑决定了Entity Framework 6的插入顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个 DBContext,我正在做以下操作:

So, I have a DBContext, and I am doing the following operations:

dbContext.SomeTables1.Add(object1)
dbContext.SomeTables2.AddRange(objectArray2)
dbContext.SomeTables3.AddRange(objectArray3)
dbContext.SaveChanges();

EF 不按此顺序插入 db 记录,它以随机顺序插入它们.要以相同的顺序插入它们,我必须在每次添加后执行 dbContext.SaveChanges() .这不是一个有效的解决方案,就我而言,完成所有插入需要 10 秒,而一次保存的随机顺序大约需要 3 秒.

The EF doesn't insert the db records in this order, it inserts them in a random order. To insert them in the same order, I have to do a dbContext.SaveChanges() after each addition. This is not an efficient solution and in my case, it is taking 10 seconds to do all my inserts, while the random order with one save takes around 3 seconds.

注意我需要正确的顺序来解决死锁问题.

N.B. I need the right order to solve a deadlock issue.

我的问题是:

  • 这个问题在 EF7 中得到解决了吗?
  • 我可以分析 EF 并确定随机顺序,但是,是否可以保证它与相同的随机顺序一致或它会在请求之间改变吗?(我可以采用我的其他代码,如果这个问题的答案是肯定的).
  • 在每次添加时,有没有比 dbContext.SaveChanges() 更好的方法来维护顺序?
  • Is this issue resolved in EF7?
  • I can profile EF and determine the random order, however, is there a guarantee that it will be consistently with the same random order or does it change between requests? (I can adopt my other code if the answer to this question is positive).
  • Is there a better way of maintaining the order than dbContext.SaveChanges() on every addition?

推荐答案

  • 您无法在 EF6 或 EF Core(最初命名为 EF7)中指定保存顺序.
  • 在 EF Core(最初命名为 EF7)中未解决该问题,因为这不是问题.
  • 如果前任相同,则顺序将相同(这可能很少发生)
  • 当您调用 SaveChanges 时,所有实体都从ProduceDynamicCommands"方法中的内部顺序排序,然后通过TryTopologicalSort"方法再次排序,该方法循环添加没有前任的命令(如果添加 A 和 B 并且 A 依赖在 B 上,则 B 将插入 A 之前)

    When you call SaveChanges, all entities are ordered from an internal order in the method "ProduceDynamicCommands" then sorted again by the method "TryTopologicalSort" which loops to add command with no predecessor left (if you add A and B and A depend on B, then B will be inserted before A)

    您需要通过批量添加来插入.

    You are left to insert by batch addition.

    由于执行插入需要 3 秒,我假设您有数千个实体,执行批量插入可能会提高您的性能,将 10 秒缩短到更少,然后可能是最初的 3 秒!

    Since it takes you 3 seconds to perform your insert, I will assume you have thousands of entities and performing bulk insert may improve your performance to reduce the 10 seconds to less, and then maybe the initial 3 seconds!

    这里有 2 个我可以推荐的库:

    Here are 2 libraries I can recommend:

    • https://efbulkinsert.codeplex.com/
      • 免费,但不适用于所有类型的关联和继承
      • https://efbulkinsert.codeplex.com/
        • FREE but doesn’t work with all kind of associations and inheritances
        • 付费但支持一切

        免责声明:我是实体框架扩展项目的所有者.

        Disclaimer: I'm the owner of the Entity Framework Extensions project.

        这篇关于什么逻辑决定了Entity Framework 6的插入顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)