Performance 使用DBContext的瓶颈是什么?

Performance 使用DBContext的瓶颈是什么?,performance,entity-framework-6,Performance,Entity Framework 6,在下面的代码段中,我首先加载的列表比其他列表花费的时间多3000毫秒。 这使我认为在第一个.ToList()上发生了某些事情,而在以后的.ToList()调用中则不会发生。可能是什么 我希望有一些东西可以提高我的表现 db是我的DBContext实例 使用(var db=new MyDataEntities()) { const int linkTypeId = 3; var sw = new Stopwatch(); s

在下面的代码段中,我首先加载的列表比其他列表花费的时间多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));


        }

应用程序中的第一个查询(或
SaveChanges
call)会导致EF初始化。有一些选项可以提高这一步骤的性能(例如预编译“视图”),但它的速度总是非常慢。

正如Ladislav所说,由于EF初始化,第一个查询的速度较慢。 也就是说,您可以在应用程序启动时通过调用以下代码强制EF在应用程序启动时初始化。


只是您希望它在什么时候发生的问题,在应用程序开始时,或者在应用程序进行第一次查询/保存更改时。

这是应用程序对数据库执行的第一次查询吗?在这种情况下,它是EF基础设施的初始化。就是这样。谢谢。您可以将它添加为答案吗。
using (var db = new MyDataEntities()) {
    context.Database.Initialize(force: true);
}