【发布时间】:2013-12-27 21:30:54
【问题描述】:
在下面的代码 sn-p 中,我首先加载的 List 比其他代码多花费大约 3000 毫秒。 这使我认为在第一个 .ToList() 上发生了某些事情,而在以后的 .ToList() 调用中没有发生。会是什么呢?
我希望有一些可以提高性能的方法。
db 是我的 DBContext 实例。
使用 (var db = new MyDataEntities())
{
const int linkTypeId = 3; var sw = new Stopwatch(); sw.Start(); // section A sw.Restart(); var qry2 = from p in db.ViewPropertyPairs where p.LinkID == JobId && p.LinkType == linkTypeId select p; this.ViewPropertyPairs = qry2.ToList(); sw.Stop(); Debug.Print(string.Format("{0} ms for to view property pair list", sw.ElapsedMilliseconds)); // Section B sw.Restart(); this.PropertyNames = db.PropertyNames.ToList(); sw.Stop(); Debug.Print(string.Format("{0} ms for propertynames", sw.ElapsedMilliseconds)); // Section C sw.Restart(); var qry =from p in db.PropertyPairs where p.LinkID == JobId && p.LinkType == linkTypeId select p ; this.PropertyPairs = qry.ToList(); sw.Stop(); Debug.Print(string.Format("{0} ms for to property pair list", sw.ElapsedMilliseconds)); }
【问题讨论】:
-
这是您的应用程序对数据库执行的第一个查询吗?在这种情况下,它是 EF 基础设施的初始化。
-
就是这样。谢谢你。你能把它添加为答案吗?
标签: performance entity-framework-6