You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to view generated sql command of a query #5106
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 basic Where query. How do you do it? Should be a super quick question...
I'm using the standard (localdb)\MSSQLLocalDB setup.
Hi Rowan,
I want to use the logger to show me only what SQL is sent to the db from a linq/ef query or creation of rows etc.
I tried (among other things) adding typeof(Microsoft.EntityFrameworkCore.Storage.DbCommandLogData).FullName
in _catgeories in MyFilteredLoggerProvider but nothing is returned (it even tells me the DbCommandLogData is obsolete, too)
I was about to post a solution for my linq to sql question. The following made it in _categories typeof(Microsoft.EntityFrameworkCore.Storage.IRelationalCommandBuilderFactory).FullName,
I agree with you, it took me some time to decrypt the debugger. I will try your new improvements, thanks a lot and yes I think it is great you re-opened it.
You can use the Asp Net Core 2.0 logger to log the SQL command in the output window.
I use it to log Linq command executed on a repository class called from a controller class.
In your Startup.cs of the startup project:
// This method gets called by the runtime.
// Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory,
IServiceProvider serviceProvider)
{
// To setup up settings of the "Logging" element specified
// in the appsettings.json
// Adds a console logger named 'Console' to the factory
loggerFactory.AddConsole();
// Adds a debug logger that is enabled for
// Microsoft.Extensions.Logging.LogLevel.Information or higher.
loggerFactory.AddDebug();
// The other configurations settings // ...
}
In your controller class:
public class SampleController : Controller
{
private readonly ISampleRepository _sampleRepository;
private readonly ILogger _logger;
public SampleController(ISampleRepository sampleRepository,
ILogger<AllarmiController> logger)
{ _sampleRepository = sampleRepository;
_logger = logger;
}
[HttpGet]
public async Task<IActionResult> SampleMethod()
{
// The method GetDataWithLinqToSQL of ISampleRepository
// use Linq to SQL.
// When you call it, you can se the SQL command parsed
// in the output window.
var sample = await _sampleRepository.GetDataWithLinqToSQL();
return Ok(sample);
}
}
Activity
Borlay commentedon Apr 19, 2016
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:
Microsoft.Data.Entity.Infrastructure;
Microsoft.Extensions.DependencyInjection;
Microsoft.Extensions.Logging;
rowanmiller commentedon Apr 19, 2016
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
ToString()from EF6.Ponant commentedon Dec 8, 2016
Hi Rowan,
I want to use the logger to show me only what SQL is sent to the db from a linq/ef query or creation of rows etc.
I tried (among other things) adding
typeof(Microsoft.EntityFrameworkCore.Storage.DbCommandLogData).FullNamein _catgeories in MyFilteredLoggerProvider but nothing is returned (it even tells me the DbCommandLogData is obsolete, too)
rowanmiller commentedon Dec 8, 2016
Here is something that works with 1.1.0
rowanmiller commentedon Dec 8, 2016
I've opened #7217 to improve this, as it has a number of issues.
Ponant commentedon Dec 8, 2016
I was about to post a solution for my linq to sql question. The following made it in _categories
typeof(Microsoft.EntityFrameworkCore.Storage.IRelationalCommandBuilderFactory).FullName,I agree with you, it took me some time to decrypt the debugger. I will try your new improvements, thanks a lot and yes I think it is great you re-opened it.
juliencousineau commentedon Apr 21, 2017
Microsoft.EntityFrameworkCore.Storage.IRelationalCommandBuilderFactory
Doesnt exist ...
ErikEJ commentedon Apr 21, 2017
@juliencousineau Of course it does... What EF Core version are you using?
juliencousineau commentedon Apr 21, 2017
My bad, I didn't add the package EntityFramework.SqlServer
Thanks !
davidbaxterbrowne commentedon Sep 22, 2017
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/
marcobarbero commentedon Jan 10, 2018
From:
https://expertcodeblog.wordpress.com/2018/01/09/entity-framewor-core-2-0-how-to-view-the-linq-sql-parsed-command-on-asp-net-core-2-0/
You can use the Asp Net Core 2.0 logger to log the SQL command in the output window.
I use it to log Linq command executed on a repository class called from a controller class.
In your Startup.cs of the startup project:
In your controller class:
I hope this is wath you need.