如何获取Entity Framework在执行savechanges()前或执行后所生成的SQL语句

2025-04-28 07:09:45
推荐回答(1个)
回答1:

您好,命名空间: System.Data.Objects
程序集: System.Data.Entity(在 System.Data.Entity.dll 中)

语法

C#
C++
F#
VB
[BrowsableAttribute(false)]
public string ToTraceString()
返回值

类型:System.String
一个 string,表示查询对数据源执行的命令。

示例

本主题中的示例基于 Adventure Works Sales Model。
C#
VB
int productID = 900;
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
// Define the object query for the specific product.
ObjectQuery productQuery =
context.Products.Where("it.ProductID = @productID");
productQuery.Parameters.Add(new ObjectParameter("productID", productID));

// Write the store commands for the query.
Console.WriteLine(productQuery.ToTraceString());
}