我似乎并没有得到LINQ2SQL查询回来的迷你探查? [英] I do not seem to get LINQ2SQL queries back with mini-profiler?

查看:171
本文介绍了我似乎并没有得到LINQ2SQL查询回来的迷你探查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2层的Web应用程序。数据访问和网站。

I have a 2 tier web application. DataAccess and WebSite.

所以在我的 dataContext.cs 上的数据访问层,我添加了包装...

So in my dataContext.cs on the DataAccess tier I added the wrapper...

  //The autogenreated context when I made the linq2sql class
  public static MyDataContext DataContext
    {
        get
        {
            //We are in a web app, use a request scope
            if (HttpContext.Current != null)
            {
                if (HttpContext.Current.Items["dc"] == null)
                {
                    MyDataContext dc = new MyDataContext ();
                    HttpContext.Current.Items["dc"] = dc;
                    return dc;
                }
                else
                    return (MyDataContext )HttpContext.Current.Items["dc"];
            }
            else
            {
                if (dataContext == null)
                    dataContext = new MyDataContext ();



                return dataContext;
            }
        }
    }

    //the method I added to the autogenreated contex in 
    //an attempt to wrap the profiler around it
    public static MyDataContext Get()
    {
        var sqlConnection = new MyDataContext().Connection;
        var profiledConnection = new StackExchange.Profiling.Data.ProfiledDbConnection(sqlConnection, MiniProfiler.Current);

        return new MyDataContext(profiledConnection);
    }

因此​​,这是什么profileConnection的样子,当它被调用,但之前的返回新MyDataContext(porofiledConnection)

和我的业务逻辑,也可以在数据访问层我确信,该​​数据库上下文全部用 DB = MyDataContext.Get()在代替<$ C创建$ C> DB =新MyDataContext();

and in my business logic also in the DataAccess tier I made sure that the db context is all created with db = MyDataContext.Get() in stead of db = new MyDataContext();

public class MyOrders(){
  private static  MyDataContext db = MyDataContext.Get();

  public static List<model> GetOrderHistory(){
      var = db.MyStoredProcedure(args) //Inspecting here before execution
      //proces result and return list of model
      }

 }

现在,在一些网页上经常拿SQL行,我可以点击他们,并检查他们。但在我浏览该网站只是显示了这一点 - 没有SQL行了吗?像这样的页面只是随机显示我duplication-但是如果我重装就消失了SQL。

Now, on some pages I used to get SQL lines and I could click on them and inspect them. But after I browsed the site it just shows this - no SQL lines any more? Like this page just randomly shows me SQL duplication- But if I reload it is gone.

和这个页面,我从来没有遇到与以前有加载时间的问题,我不能确定它使用的SQL。分析器上

And on this page that I never ran with the profiler before that has loading time issues I cannot identify the SQL it used.

我错过了什么?是SQL缓存?我一直想看到的SQL即使LINQ2SQL其缓存或什么的。我做了什么错?

Did I miss something? Is the SQL cached? I always want to see the SQL even if Linq2Sql caches it or whatever. What did I do wrong?

推荐答案

您的 MyOrders 类中的静态数据上下文。 的DataContext 有一个内部缓存中跟踪实体的变化,避免在一个商业交易往返数据库。保持它作为静态的,这意味着内部高速缓存将增加暂时并没有正确地释放。这可能是探查消失查询的原因。你也可能会面临一个问题,当有多个用户从多个线程,而且很可能内存泄漏访问相同的情况下。

You have a static data context in your MyOrders class. DataContext has an internal cache inside to track changes of entities and avoid round trip to database in one business transaction. Keeping it as static, means internal cache will be increasing for the time being and is not released properly. This may be the reason of disappeared queries in profiler. Also you may face a problem when multiple users will access the same context from multiple threads, and probably memory leaks.

MSDN 的说明:

在一般情况下,一个的DataContext 实例设计到最后一个单位   工作,然而你的应用程序定义的术语,指的DataContext   轻便,也不贵创建。一个典型的LINQ to SQL   应用程序在方法范围或作为创建的DataContext 实例   短命类,再present相关的逻辑组成员   数据库操作。

In general, a DataContext instance is designed to last for one "unit of work" however your application defines that term. A DataContext is lightweight and is not expensive to create. A typical LINQ to SQL application creates DataContext instances at method scope or as a member of short-lived classes that represent a logical set of related database operations.

一个更多

不要尝试重用的DataContext 的实例。每个的DataContext   维护状态(包括身份缓存)一个特定   编辑/查询会话。以获得基于当前状态的新实例   数据库时,使用一个新的的DataContext

Do not try to reuse instances of DataContext. Each DataContext maintains state (including an identity cache) for one particular edit/query session. To obtain new instances based on the current state of the database, use a new DataContext.

更多的细节,你可以在里克施特拉尔的文章的 LINQ到SQL的DataContext生命周期管理

More details you can find in the Rick Strahl's article Linq to SQL DataContext Lifetime Management.

这篇关于我似乎并没有得到LINQ2SQL查询回来的迷你探查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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