ahjesus 捕获entity framework生成的sql语句

SQL Server,网上这上头的质感很少,找到三个方可用的

http://code.msdn.microsoft.com/EFProviderWrappers

里面有dll能够下载,有学科,可是是E文的。

 

在Entity Framework中要翻开毕竟最终转译的SQL
command是甚麼出色艰苦,因為EF本人并不曾提供像Linq2sql的Log属性来达到此点,那恐怕是因為Linq2sql比较单纯只对SQL
Server,而EF在规划上是与资料库无关.

近来Entity
Framework只可以通过ObjectQuery的ToTraceString()来输出转译后的查询SQL语法.但对於SaveChange这类新增修改没有主意能够出口到底实际上的SQL
command是甚麼.

往常要追踪的点子差不多正是从SQL
Profiler那类资料库附加的软体於资料库端追踪输出,但因為资料库平日不太大概单单唯有一隻程式使用,在追踪上稍稍麻烦.

 

 

幸亏有人找出了化解之道并实作Library提供给大家使用.专案参考http://code.msdn.microsoft.com/EFProviderWrappers

她的做法是从EntityConnection初始,底下图的左侧是原本的EF架构,而右手则使此Library的做法,中间多了一层Wrapping
Provider.

 

SQL Server 1

 

再来介绍该怎么使用此Library

1.请先至网站下载档案,因為下载下来為SourceCode所以请先用VS编写翻译过.

2.编写翻译后搜索EFTracingProvider目录底下的bindebug有七个dll,EFProviderWrapperToolkit.dll与EFTracingProvider.dll.

3.将那多个dll复製到你想要的目录.

4.开啟EF专案,使用加入参考格局投入此1个dll.

 

SQL Server 2

5.再来正是何许掛载Wrapping Provider,预设情况用EDM tool
desinger设计好模型后,在VS中会產生.edmx与.designer.cs档案,而.designer.cs中会產生对应的ObjectContext
class, 而ObjectContext class则是行使EDM tool
desinger时所输入的称呼,预设会是xxxEntities.

而掛载格局正是继承此class修改其建构式,譬如底下的范例是运用EDM建立3个TestEntities的ObjectContext.
新增二个class档案,命名為ExtendedTestEntities.cs修改如下,

 

    public partial class ExtendedTestEntities : TestEntities

    {

        private TextWriter logOutput;

 

        public ExtendedTestEntities()

            : this("name=TestEntities")

        {

        }

 

        public ExtendedTestEntities(string connectionString)

            : base(EntityConnectionWrapperUtils.CreateEntityConnectionWithWrappers(

                    connectionString,

                    "EFTracingProvider"

            ))

        {

        }

 

        #region Tracing Extensions

 

        private EFTracingConnection TracingConnection

        {

            get { return this.UnwrapConnection<EFTracingConnection>(); }

        }

 

        public event EventHandler<CommandExecutionEventArgs> CommandExecuting

        {

            add { this.TracingConnection.CommandExecuting += value; }

            remove { this.TracingConnection.CommandExecuting -= value; }

        }

 

        public event EventHandler<CommandExecutionEventArgs> CommandFinished

        {

            add { this.TracingConnection.CommandFinished += value; }

            remove { this.TracingConnection.CommandFinished -= value; }

        }

 

        public event EventHandler<CommandExecutionEventArgs> CommandFailed

        {

            add { this.TracingConnection.CommandFailed += value; }

            remove { this.TracingConnection.CommandFailed -= value; }

        }

 

        private void AppendToLog(object sender, CommandExecutionEventArgs e)

        {

            if (this.logOutput != null)

            {

                this.logOutput.WriteLine(e.ToTraceString().TrimEnd());

                this.logOutput.WriteLine();

            }

        }

 

        public TextWriter Log

        {

            get { return this.logOutput; }

            set

            {

                if ((this.logOutput != null) != (value != null))

                {

                    if (value == null)

                    {

                        CommandExecuting -= AppendToLog;

                    }

                    else

                    {

                        CommandExecuting += AppendToLog;

                    }

                }

 

                this.logOutput = value;

            }

        }

 

 

        #endregion

    }

以上程式码若要套用於你的程式只要修改TestEntities相关描述即可,剩下的能够一贯Copy.

 

末段在程式中应用格局如下

 

            EFTracingProviderConfiguration.RegisterProvider();

 

            using (ExtendedTestEntities context = new ExtendedTestEntities())

            {                

                context.CommandExecuting += (sender, e) =>

                {

                    string s = e.ToTraceString();

                };

 

                context.CommandFinished += (sender, e) =>

                {

                    string s = e.ToTraceString();

                };

 

                ...

            }

经过CommandExecuting与CommandFinished事件则可拦截到最终EF下达SQL命令开首实施与实践后,其它还有执行破产的轩然大波,而通过callback函式的参数之ToTraceString()即可获得完整的转译后之SQL
command.

 

那边要小心一点,第①行的EFTracingProviderConfiguration.RegisterProvider();

是用来註册那个Library的Provider,因為这几个Library除了Tracer
SQL外还提供别的的作用,譬如说 Cache机制,都以由此Wrapping
Provider来完结,如要了然能够参照官方网站之表明文件.

而除了这么些之外在程式中选择,也可透过.net
config机制来註册,在相关config中参预如下的叙说即可

 

<system.data>
  <DbProviderFactories>
    <add name="EF Tracing Data Provider"
         invariant="EFTracingProvider"
         description="Tracing Provider Wrapper"
         type="EFTracingProvider.EFTracingProviderFactory, EFTracingProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def642f226e0e59b" />
    <add name="EF Generic Provider Wrapper"
         invariant="EFProviderWrapper"
         description="Generic Provider Wrapper"
         type="EFProviderWrapperToolkit.EFProviderWrapperFactory, EFProviderWrapperToolkit, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def642f226e0e59b" />
  </DbProviderFactories>
</system.data>

相关文章