[英]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 Load
或Eager Load
来解决这些问题。
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
您的方法DbSet
像db.Persons.Include(p => p.Cars).Include(p => p.Pets).Include(p => p.Children).Where(p => p.Id == personId);
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:
需要检查代码中的某些内容,以确保您是:
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()
等...仅执行一次查询,并在需要重复查询时阻止对数据库的相同请求。 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.