Log Insert and Update Command sql query in Entity Framework

c# entity-framework entity-framework-6

Accepted Answer

You'll need to collect lines and find the end of the SQL query. Whenever the line starts with-- It is clear that the SQL query has not ended. You may trap the comments (and parameters) if you'd want to.-- Completed line. thereby, callingWriteDetailed directly call a method to gather and evaluate the SQL string produced by EF, and callWriteDetailed yourself as necessary.

As youLOGS add the following to class:

private static StringBuilder sb = new StringBuilder();

public static void SqlLineGenerated(string line)
{
    sb.Append(line);

    if (line.StartsWith("-- Completed", StringComparison.OrdinalIgnoreCase))
    {
        WriteDetailed(sb.ToString());
        sb = new StringBuilder();
    }
}

Next, set up the logging as follows:

context.Database.Log = msg => LOGS.SqlLineGenerated(msg);

Now you may proceed.

2
8/29/2017 2:27:19 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow