1、关于EF6的记录Sql语句,一个老生长谈的问题。 他生成的sql语句实在是烂,大家都这样说2、EF6 更新删除不方便,没有批量操作。所以,有人出了EF6.Extend 大家用起来也很爽 基于以上两点,我也尝试着使用 EF6.Extend 。本以为可以很好的, ...
1、关于EF6的记录Sql语句,一个老生长谈的问题。 他生成的sql语句实在是烂,大家都这样说
2、EF6 更新删除不方便,没有批量操作。所以,有人出了EF6.Extend 大家用起来也很爽
基于以上两点,我也尝试着使用 EF6.Extend 。本以为可以很好的,很美好。没有想到我遇到了一个大问题。
我需要 通过程序记录 EF执行的Sql语句,当然也包括 EF6.Extend 执行的Sql语句。(不是通过SqlProfiler)
在网上查找,发现 了一篇文章,我就这样抄下来了。(太匆忙解决问题,忘记了哪一篇)
继承了 System.Data.Entity.Infrastructure.Interception.DbCommandInterceptor ,实现相关方法。 然后在Main方法(程序入口)进行添加 DbInterception.Add(new EFIntercepterLogging());
1 using System; 2 using System.Collections.Generic; 3 using System.Data.Entity.Infrastructure.Interception; 4 using System.Diagnostics; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace EF_Sqlite10 {11 class EFIntercepterLogging : DbCommandInterceptor12 {13 private readonly Stopwatch _stopwatch = new Stopwatch();14 public override void ScalarExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)15 {16 base.ScalarExecuting(command, interceptionContext);17 _stopwatch.Restart();18 }19 public override void ScalarExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)20 {21 _stopwatch.Stop();22 if (interceptionContext.Exception != null)23 {24 Trace.TraceError("Exception:{1} \r\n --> Error executing command: {0}", command.CommandText, interceptionContext.Exception.ToString());25 }26 else27 {28 get='_blank'>string txt=string.Format("\r\n执行时间:{0} 毫秒\r\n-->ScalarExecuted.Command:{1}\r\n", _stopwatch.ElapsedMilliseconds, command.CommandText);29 Console.WriteLine(txt);30 Trace.TraceInformation(txt);31 }32 base.ScalarExecuted(command, interceptionContext);33 }34 public override void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)35 {36 base.NonQueryExecuting(command, interceptionContext);37 _stopwatch.Restart();38 }39 public override void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)40 {41 42 _stopwatch.Stop();43 if (interceptionContext.Exception != null)44 {45 Trace.TraceError("Exception:{1} \r\n --> Error executing command:\r\n {0}", command.CommandText, interceptionContext.Exception.ToString());46 }47 else48 { 49 50 string txt = string.Format("\r\n执行时间:{0} 毫秒\r\n-->ScalarExecuted.Command:{1}\r\n", _stopwatch.ElapsedMilliseconds, command.CommandText);51 Console.WriteLine(txt);52 Trace.TraceInformation(txt);53 }54 base.NonQueryExecuted(command, interceptionContext);55 }56 public override void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)57 {58 base.ReaderExecuting(command, interceptionContext);59 _stopwatch.Restart();60 }61 public override void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)62 {63 _stopwatch.Stop();64 if (interceptionContext.Exception != null)65 {66 Trace.TraceError("Exception:{1} \r\n --> Error executing command:\r\n {0}", command.CommandText, interceptionContext.Exception.ToString());67 }68 else69 {70 string txt = string.Format("\r\n执行时间:{0} 毫秒\r\n-->ScalarExecuted.Command:{1}\r\n", _stopwatch.ElapsedMilliseconds, command.CommandText);71 Console.WriteLine(txt);72 Trace.TraceInformation(txt);73 }74 base.ReaderExecuted(command, interceptionContext);75 }76 77 78 }79 }
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:关于EF6的记录Sql语句 与 EntityFramework.Extend 的诟病
关键词:sql
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。