문제

LINQ에서 SQL을 사용하고 있습니다. 나는 .submitchanges () 'ing에있는 DataContext가 있습니다. Identity 필드를 삽입하는 오류가 있으며이 Identity 필드를 삽입하는 데 사용중인 쿼리를보고 싶습니다.

QuickWatch 내에서 쿼리 자체가 보이지 않습니다. 디버거 내에서 어디서 찾을 수 있습니까?

도움이 되었습니까?

해결책

실제로 귀하의 질문에 대한 매우 간단한 답변이 있습니다.

시계 창에 붙여 넣기 만하면됩니다

((System.Data.Objects.ObjectQuery)myLinqQueryVar).ToTraceString()

다른 팁

많은 사람들이 자신의 "디버그 라이터"를 작성하고 다음과 같이 첨부했습니다.

// Add this class somewhere in your project...
class DebugTextWriter : System.IO.TextWriter {
   public override void Write(char[] buffer, int index, int count) {
       System.Diagnostics.Debug.Write(new String(buffer, index, count));
   }

   public override void Write(string value) {
       System.Diagnostics.Debug.Write(value);
   }

   public override Encoding Encoding {
       get { return System.Text.Encoding.Default; }
   }
}

// Then attach it to the Log property of your DataContext...
myDataContext.Log = new DebugTextWriter()

Linq-to-SQL이 Visual Studio의 디버그 창에 수행하는 모든 것을 출력합니다.

추가로 포트만의 대답, 콘솔 애플리케이션이라면 간단합니다.

myDataContext.Log = Console.Out;

또는 LINQ2SQL 프로파일 러와 같은 것을 사용할 수 있으며, 이는 다소 우수한 도구이며 실제로 작업에 적합한 도구입니다.

LINQ에서 SQL 프로파일 러 - LINQ에서 SQL에 대한 실시간 비주얼 디버거

SQL 프로파일 러를 실행하십시오. SQL 명령 텍스트를 포함하여 데이터베이스에 모든 트래픽이 표시됩니다.

FooDataContext dc = new FooDataContext();

StringBuilder sb = new StringBuilder();
dc.Log = new StringWriter(sb);

var result=from r in dc.Tables select d;

.....
string query=sb.ToString();

LINQ에서 SQL 프로파일 러 가이 작업에 적합한 도구라는 데 동의합니다. 그러나 돈을 쓰고 싶지 않거나 간단한 일을해야한다면 디버그 텍스트 작가 접근 방식이 마음에 듭니다.

이 질문을 읽은 후 나는 더 강력한 것을 찾아 갔다. 그것은 또한 Damien Guard가 밝혀졌습니다 아주 좋은 기사를 썼습니다 메모리, 디버그, 파일, 여러 대상 또는 간단한 대표를 사용하는 것과 같은 다른 것들을 다루기 위해 다른 작가를 구축하는 것에 대해.

나는 그의 아이디어 몇 개를 사용하고 한 명 이상의 대의원을 처리 할 수있는 ActionTextWriter를 작성했으며 여기에서 공유 할 것이라고 생각했습니다.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Writers
{
    public class ActionTextWriter : TextWriter
    {
        protected readonly List<Action<string>> Actions = new List<Action<string>>();

        public ActionTextWriter(Action<string> action)
        {
            Actions.Add(action);
        }

        public ActionTextWriter(IEnumerable<Action<string>> actions)
        {
            Actions.AddRange(actions);
        }

        public ActionTextWriter(params Action<string>[] actions)
        {
            Actions.AddRange(actions);
        }

        public override Encoding Encoding
        {
            get { return Encoding.Default; }
        }

        public override void Write(char[] buffer, int index, int count)
        {
            Write(new string(buffer, index, count));
        }

        public override void Write(char value)
        {
            Write(value.ToString());
        }

        public override void Write(string value)
        {
            if (value == null)
            {
                return;
            }

            foreach (var action in Actions)
            {
                action.Invoke(value);
            }
        }
    }
}

원하는만큼 많은 동작을 추가 할 수 있습니다. 이 예제는 debug.write를 통해 로그 파일과 Visual Studio의 콘솔에 기록됩니다.

// Create data context
var fooDc = new FooDataContext();

// Create writer for log file.
var sw = new StreamWriter(@"C:\DataContext.log") {AutoFlush = true};

// Create write actions.
Action<string> writeToDebug = s => Debug.Write(s);
Action<string> writeToLog = s => sw.Write(s);

// Wire up log writers.
fooDc.Log = new ActionTextWriter(writeToDebug, writeToLog);

물론 커프에서 더 간단한 것을 만들고 싶다면 항상 연장하다 ActionTextWriter ... 일반적인 접근 방식을 작성하고 재사용하십시오.

using System.Diagnostics;
using System.IO;

namespace Writers
{
    public class TraceTextWriter : ActionTextWriter
    {
        public TraceTextWriter()
        {
            Actions.Add(s => Trace.Write(s));
        }
    }

    public class FileTextWriter : ActionTextWriter
    {
        public FileTextWriter(string path, bool append = false)
        {
            var sw = new StreamWriter(path, append) {AutoFlush = true};
            Actions.Add(sw.Write);
        }
    }
}

자세한 설명은 다음과 같습니다. http://debugmode.net/2011/06/26/logging-in-linq-to-sql/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top