如何查看LINQ生成的原始查询



我想查看生成的sql以使用Console.WriteLine((进行调试;在Visual studio Code中,使用Console.WriteLine((,我看不到原始查询;

var result = from employee in db.EmployeeUsers
...
select employee.id..;
string sql = result.ToString();
Console.WriteLine(sql);

但它看起来像

Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[<>f__AnonymousType1641]

如何看到这样的原始查询?

[SELECT employee.id FROM ... WHERE...;]

我先设置了这个,

"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
,"Microsoft.EntityFrameworkCore.Database.Command": "Information"
}
},

但我没发现是不是我设置错了。然后我设置

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.LogTo(Console.WriteLine);

但它显示了每个原始查询。

我只是希望查询只打印在我在Visual Studio代码终端中指定的部分上。

显然,您可以调用.ToQueryString((或使用Query属性。

https://stackoverflow.com/a/68797954/1974021

var result = from employee in db.EmployeeUsers
...
select employee.id..;
string sql = result.ToQueryString();

最新更新