-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to view generated sql command of a query #5106
Comments
I don't know about current query sql but if you need just log sql queries, i have extension like this:
you need some namespaces: |
More info on logging here; http://docs.efproject.net/en/latest/miscellaneous/logging.html (similar to what @Loat described) At this stage EF Core doesn't have an equivalent to |
Hi Rowan, |
Here is something that works with 1.1.0 public class SqlLoggerProvider : ILoggerProvider
{
public ILogger CreateLogger(string categoryName)
{
if (categoryName == typeof(Microsoft.EntityFrameworkCore.Storage.IRelationalCommandBuilderFactory).FullName)
{
return new SqlLogger(categoryName);
}
return new NullLogger();
}
public void Dispose()
{ }
private class SqlLogger : ILogger
{
private string _test;
public SqlLogger(string test)
{
_test = test;
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (eventId.Id == (int)RelationalEventId.ExecutedCommand)
{
var data = state as IEnumerable<KeyValuePair<string, object>>;
if (data != null)
{
var commandText = data.Single(p => p.Key == "CommandText").Value;
Console.WriteLine(commandText);
}
}
}
public IDisposable BeginScope<TState>(TState state)
{
return null;
}
}
private class NullLogger : ILogger
{
public bool IsEnabled(LogLevel logLevel)
{
return false;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{ }
public IDisposable BeginScope<TState>(TState state)
{
return null;
}
}
} |
I've opened #7217 to improve this, as it has a number of issues. |
I was about to post a solution for my linq to sql question. The following made it in _categories |
Microsoft.EntityFrameworkCore.Storage.IRelationalCommandBuilderFactory Doesnt exist ... |
@juliencousineau Of course it does... What EF Core version are you using? |
My bad, I didn't add the package EntityFramework.SqlServer Thanks ! |
BTW, I was annoyed by EF Core logging, and wrote a little helper to get back simple EF6-style logging. https://blogs.msdn.microsoft.com/dbrowne/2017/09/22/simple-ef6-style-logging-for-ef-core/ |
You can use the Asp Net Core 2.0 logger to log the SQL command in the output window. In your Startup.cs of the startup project:
In your controller class:
I hope this is wath you need. |
I could have sworn I was able to do
query.ToString()
to see the generated SQL query, but that's not working in EF7 on a basicWhere
query. How do you do it? Should be a super quick question...I'm using the standard (localdb)\MSSQLLocalDB setup.
The text was updated successfully, but these errors were encountered: