【问题标题】:view generated SQL for Entity Framewok SaveChanges command in Visual studio?在 Visual Studio 中查看为实体框架保存更改命令生成的 SQL?
【发布时间】:2012-03-02 15:31:16
【问题描述】:

我可以看到 Entity Framework 生成的 SQL 用于 Visual Studio 中的选择操作,但不能用于插入、更新和删除。调试时如何在 Visual Studio 中查看为“DataContext.SaveChanges”命令生成的 SQL?

【问题讨论】:

  • 猜你没有完整版的SqlServer
  • 我使用的是完整版的 SQL Server,但我没有使用 SQL Server Profiler 的权限。但我可以在 Visual Studio 中看到选择的 sql,但看不到更新。为什么?我看到了关闭这篇文章的投票,因为我想知道是否有 SQL Server Profiler 的替代解决方案。
  • 该链接不涉及更新。只选择

标签: visual-studio-2010 entity-framework linq-to-entities entity-framework-4.1


【解决方案1】:

如果您有 Visual Studio Ultimate,您可以在 Intellitrace 中看到更新和插入。只需在调用 SaveChanges 后立即设置断点即可。

http://www.youtube.com/watch?v=fLBpZNXs-Lw

如果您在 Web 项目中使用它,您也可以使用 mini-profiler。

http://miniprofiler.com/

【讨论】:

    【解决方案2】:

    我发现了一些非常简单的东西:

    context.Database.Log = x => System.Diagnostics.Debug.WriteLine(x);
    

    【讨论】:

    【解决方案3】:

    在 MSDN 论坛上查看 this thread;具体来说,g_yordanov 的帖子。他提供了一些代码,可以为 EF 数据上下文中的所有更改检索相应的 SQL 语句。

    作为免责声明,此代码涉及对 EF 内部的反映,并且可能会在未来的版本中中断。但目前,它在我们所有的 EF 应用程序中都能完美运行。

    这是代码,供参考,以防链接消失。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    using System.Data.Objects;
    using System.Data.Common;
    using System.Data.EntityClient;
    using System.Collections;
    
    namespace EntityExtensionMethods
    {
        public static class CustomExtensions
        {
            private static readonly string entityAssemblyName =
                "system.data.entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
    
            public static string ToTraceString(this IQueryable query)
            {
                System.Reflection.MethodInfo toTraceStringMethod = query.GetType().GetMethod("ToTraceString");
    
                if (toTraceStringMethod != null)
                    return toTraceStringMethod.Invoke(query, null).ToString();
                else
                    return "";
            }
    
            public static string ToTraceString(this ObjectContext ctx)
            {
                Assembly entityAssemly = Assembly.Load(entityAssemblyName);
    
                Type updateTranslatorType = entityAssemly.GetType(
                    "System.Data.Mapping.Update.Internal.UpdateTranslator");
    
                Type functionUpdateCommandType = entityAssemly.GetType(
                    "System.Data.Mapping.Update.Internal.FunctionUpdateCommand");
    
                Type dynamicUpdateCommandType = entityAssemly.GetType(
                    "System.Data.Mapping.Update.Internal.DynamicUpdateCommand");
    
                object[] ctorParams = new object[]
                            {
                                ctx.ObjectStateManager,
                                ((EntityConnection)ctx.Connection).GetMetadataWorkspace(),
                                (EntityConnection)ctx.Connection,
                                ctx.CommandTimeout
                            };
    
                object updateTranslator = Activator.CreateInstance(updateTranslatorType,
                    BindingFlags.NonPublic | BindingFlags.Instance, null, ctorParams, null);
    
                MethodInfo produceCommandsMethod = updateTranslatorType
                    .GetMethod("ProduceCommands", BindingFlags.Instance | BindingFlags.NonPublic);
                object updateCommands = produceCommandsMethod.Invoke(updateTranslator, null);
    
                List<DbCommand> dbCommands = new List<DbCommand>();
    
                foreach (object o in (IEnumerable)updateCommands)
                {
                    if (functionUpdateCommandType.IsInstanceOfType(o))
                    {
                        FieldInfo m_dbCommandField = functionUpdateCommandType.GetField(
                            "m_dbCommand", BindingFlags.Instance | BindingFlags.NonPublic);
    
                        dbCommands.Add((DbCommand)m_dbCommandField.GetValue(o));
                    }
                    else if (dynamicUpdateCommandType.IsInstanceOfType(o))
                    {
                        MethodInfo createCommandMethod = dynamicUpdateCommandType.GetMethod(
                            "CreateCommand", BindingFlags.Instance | BindingFlags.NonPublic);
    
                        object[] methodParams = new object[]
                        {
                            updateTranslator,
                            new Dictionary<long, object>()
                        };
    
                        dbCommands.Add((DbCommand)createCommandMethod.Invoke(o, methodParams));
                    }
                    else
                    {
                        throw new NotSupportedException("Unknown UpdateCommand Kind");
                    }
                }
    
    
                StringBuilder traceString = new StringBuilder();
                foreach (DbCommand command in dbCommands)
                {
                    traceString.AppendLine("=============== BEGIN COMMAND ===============");
                    traceString.AppendLine();
    
                    traceString.AppendLine(command.CommandText);
                    foreach (DbParameter param in command.Parameters)
                    {
                        traceString.AppendFormat("{0} = {1}", param.ParameterName, param.Value);
                        traceString.AppendLine();
                    }
    
                    traceString.AppendLine();
                    traceString.AppendLine("=============== END COMMAND ===============");
                }
    
                return traceString.ToString();
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      我知道这很旧,但它是我搜索中出现的第一个链接,所以我想我会发布对我来说最简单的解决方案是使用 SQL Profiler per:

      String or binary data would be truncated.The statement has been terminated

      糟糕...刚刚看到 OP 无法访问 Profiler,但该链接仍然可以为那些可以访问的人提供指导!

      【讨论】:

      • 您可以将此添加为评论,但答案应直接尝试解决 OP 的问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多