实体框架的初始化是缓慢的 - 我能做些什么更快的引导呢? [英] Entity Framework initialization is SLOW -- what can I do to bootstrap it faster?

查看:300
本文介绍了实体框架的初始化是缓慢的 - 我能做些什么更快的引导呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的EF 4.3.1模型有200多名表。初始启动是可怕的,几分钟的时间。一个DotTrace捕获配置文件意味着深的框架,一些可怕的算法/可扩展性的选择,就证明了数以百万计的呼叫来了许多方法那里和3600万IEnumerable.Contains()调用。这里是一个片段,这是所有的数据库上做了第一个查询(将来的查询不这样做,并罚款)。

My EF 4.3.1 model has 200-odd tables. Initial startup is horrible, several minutes. A DotTrace-captured profile implies some terrible algorithm/scalability choices deep in the framework, as evidenced by the millions of calls to a number of methods down there and the 36 million IEnumerable.Contains() calls. Here is a snippet, this is all triggered by the first query done on the database (future queries don't do this and are fine).

我能做些什么,以我的模型,使这个痛苦少?我可以precompile这个不知何故?好,可以在EF团队,请解决这些问题,或者开源的框架,所以我可以吗?或至少​​修复 Warapper 的拼写? :)

What can I do to my model to make this less painful? Can I precompile this somehow? Better, can the EF team please address these issues or open source the framework so I can? Or at least fix the spelling of Warapper? :)

编辑:一个特定的EF调用触发这个基本上变种DB =新MyDbContext(); db.Personnel.Where(A => a.Login ==登录).SingleOrDefault(); 。还有一个EF迁移种子()AddOrUpdate有效地产生相同的堆栈。富勒堆栈跟踪,这可能会给多一点的情况下,是在这里:富勒堆栈跟踪

One specific EF call that triggers this is basically var db = new MyDbContext(); db.Personnel.Where(a => a.Login == login).SingleOrDefault();. Also an EF Migrations Seed() AddOrUpdate generates effectively the same stack. The fuller stack trace, which may give a little more context, is here: Fuller Stack Trace

编辑:一些相关链接:

  • MSDN: Performance Considerations (Entity Framework) (thanks to @AakashM)
  • MSDN: EF Power Tools
  • SO: Entity Framework 4.1 for large number of tables (715)

EDIT2:现在,他们只需开源中的code,看来这行:

Now that they just open sourced the code, it appears that this line:

//Filter the 1:1 foreign key associations to the ones relating the sets used in these cell wrappers.
oneToOneForeignKeyAssociationsForThisWrapper =
    oneToOneForeignKeyAssociationsForThisWrapper.Where(
        it => (it.AssociationEndMembers.All(endMember => entityTypes.Contains(endMember.GetEntityType()))));

是,需要一些工作的人。它使用的是为O(n ^ 2)算法时,它可能不会有,但我还没有仔细地看了看呢。

is the one that needs some work. It's using an O(n^2) algorithm when it probably doesn't have to, but I haven't looked closely yet.

EDIT3:令人高兴的是,它看起来像在EF6工作是解决这个code:<一href=\"http://entityframework.$c$cplex.com/discussions/396130\">http://entityframework.$c$cplex.com/discussions/396130

Happily, it looks like work in EF6 is fixing this code: http://entityframework.codeplex.com/discussions/396130

推荐答案

在pre EF6视图生成被称为是更大的机型缓慢。对于现在的解决方案是使用pregenerated意见。这样,你的看法产生在设计时,并避免在运行这项工作。要做到这一点下载EF电动工具并选择优化实体数据模型。它将一个C#文件添加到项目中包含的观点。不利的一面是,你需要做的每一次你的模型的变化。注:生成工具将需要大约相同数量的花费的时间产生的观点在运行时的意见(所以有时你需要耐心等待)。这是一个关于EF电动工具可能有帮助的帖子:的http://blogs.msdn.com/b/adonet/archive/2011/05/18/ef-power-tools-ctp1-released.aspx

In pre EF6 view generation is known to be slow for bigger models. For now the solution is to use pregenerated views. This way you generate views at design time and are avoiding this work at runtime. To do that download EF power tools and select "Optimize Entity Data Model". It will add a C# file to your project that contains views. The down side is that you will need to do it each time your model changes. Note: to generate views with the tool it will take about the same amount of time it takes to generate views at runtime (so sometimes you need to be patient). Here is a post about EF Power Tools that might be helpful: http://blogs.msdn.com/b/adonet/archive/2011/05/18/ef-power-tools-ctp1-released.aspx

修改

最近,我创建了一个不同的解决方案,更加方便使用(注意它只能在EF6) - <一个href=\"http://blog.3d-logic.com/2013/12/14/using-$p$p-generated-views-without-having-to-$p$p-generate-views-ef6/\" rel=\"nofollow\">http://blog.3d-logic.com/2013/12/14/using-$p$p-generated-views-without-having-to-$p$p-generate-views-ef6/

Recently I created a different solution that is much more convenient to use (note it only works on EF6) - http://blog.3d-logic.com/2013/12/14/using-pre-generated-views-without-having-to-pre-generate-views-ef6/

这篇关于实体框架的初始化是缓慢的 - 我能做些什么更快的引导呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆