我想查看生成的 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);
但它顯示了每個原始查詢。
我只是希望 Query 只列印在我在 Visual Studio Code Terminal 中指定的部分。
uj5u.com熱心網友回復:
顯然您可以呼叫 .ToQueryString() 或使用 Query 屬性。
https://stackoverflow.com/a/68797954/1974021
var result = from employee in db.EmployeeUsers
...
select employee.id..;
string sql = result.ToQueryString();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/522820.html
標籤:C#林克
