简体   繁体   English

实体框架在加载的DynamicProxies上挂起

[英]Entity Framework hangs on DynamicProxies loaded

I am trying to diagnose a severe performance issue in my application. 我正在尝试诊断应用程序中的严重性能问题。 It's been almost a week and I am not sure where or what is causing it, except today I noticed the following "Loaded 'EntityFrameworkDynamicProxies' line was holding up things for almost 2 minutes. What is this? Why is it so slow and how can I improve it? 已经快一个星期了,我不确定是什么原因或什么原因造成的,除了今天我注意到以下“ Loaded'EntityFrameworkDynamicProxies”行将内容拖延了将近2分钟。这是什么?为什么这么慢,怎么办?我改善了吗?

在此处输入图片说明

When creating instances of POCO entity types, the Entity Framework often creates instances of a dynamically generated derived type that acts as a proxy for the entity. 在创建POCO实体类型的实例时,实体框架通常会创建动态生成的派生类型的实例,以作为实体的代理。 This proxy overrides some virtual properties of the entity to insert hooks for performing actions automatically when the property is accessed. 该代理会覆盖实体的某些虚拟属性,以插入用于在访问属性时自动执行操作的挂钩。 For example, this mechanism is used to support lazy loading of relationships. 例如,此机制用于支持关系的延迟加载。

Source : Data Developer Center 资料来源: 资料开发人员中心

You can disable it by setting this line this.Configuration.ProxyCreationEnabled = false; 您可以通过this.Configuration.ProxyCreationEnabled = false;下行设置为this.Configuration.ProxyCreationEnabled = false;来禁用它this.Configuration.ProxyCreationEnabled = false; in your DbContext constructor. 在您的DbContext构造函数中。

If disabled, you may encounter some problems somewhere in your application when existing code rely on Lazy Loading to load related data. 如果禁用此功能,则当现有代码依赖Lazy Loading来加载相关数据时,您可能会在应用程序中的某些地方遇到问题。 You have to fix those problems by using Explicit Load or Eager Load . 您必须使用Explicit LoadEager Load来解决这些问题。

  • Eager Loading by using Include method of your DbSet like db.Persons.Include(p => p.Cars).Include(p => p.Pets).Include(p => p.Children).Where(p => p.Id == personId); 预先加载通过使用Include您的方法DbSetdb.Persons.Include(p => p.Cars).Include(p => p.Pets).Include(p => p.Children).Where(p => p.Id == personId);
  • Explicit Loading by using the change tracker and Load method on your entry like this : db.entry(person).Collection(p => p.Cars).Load(); 通过在条目上使用更改跟踪器和Load方法来进行显式加载 ,如下所示: db.entry(person).Collection(p => p.Cars).Load(); for collection navigational property or db.entry(person).Property(p => p.Home).Load(); 用于集合导航属性或db.entry(person).Property(p => p.Home).Load(); for simple navigational property. 用于简单的导航属性。

Lazy Loading, Explicit Loading or Eager Loading, there is no silver bullet for improving your application performance if you are not using EF correctly. 延迟加载,显式加载或急切加载,如果没有正确使用EF,则没有提高应用程序性能的灵丹妙药。 There are some things to check in your code to make sure you are: 需要检查代码中的某些内容,以确保您是:

  • using ToList() , ToArray() etc... to execute your query only once and prevent same request to the database when you need to reiterate on the query. 使用ToList()ToArray()等...仅执行一次查询,并在需要重复查询时阻止对数据库的相同请求。
  • not filtering on client side. 没有在客户端过滤。 I mean make sure you create your query with correct filters before sending query to your database. 我的意思是在将查询发送到数据库之前,请确保使用正确的过滤器创建查询。
  • creating SQL Views for queries if you see that the generated SQLs by EF are not efficient. 如果看到EF生成的SQL效率不高,则为查询创建SQL视图。 Copy the generated SQL and analyze the Execution Plan on SSMS. 复制生成的SQL并分析SSMS上的执行计划。

There are many things that can be improved depending on what your application is doing and need. 根据您的应用程序在做什么和需要,有许多可以改进的地方。 You can use cache to avoid future request etc... 您可以使用缓存来避免将来的请求等。

For me, the issue was due to my poorly used async/await code! 对我来说,问题出在我的异步/等待代码使用不当! As per the OP, it was hanging for the same reason, but I got 0 error messages. 根据OP,它由于相同的原因而挂起,但是我收到了0条错误消息。

The only way I could resolve it was sticking a break point where the application starts and stepping through. 我能解决的唯一方法是在应用程序启动并逐步执行时插入一个断点。 It wasn't super quick, but it wasn't a big task either. 这不是超级快,但这也不是什么大任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM