Entity Framework 與關聯式資料庫 SQL

Entity Framework 讓我們可以直接透過 LINQ 或是 Entity SQL 進行資料物件搜尋,然後為開發人員建立所需的 SQL ,如果想要看真正送進去資料庫的 SQL ,可以透過呼叫 ObjectQuery 物件的 ToTraceString 方法來取得 。

考慮以下的程式碼:

KTBOOKSEntities ke = new KTBOOKSEntities();
var v = from b in ke.Books
 where b.BookPrice <>700
ObjectQuery objv = ((ObjectQuery)v);
msg+="\n\n SQL ... \n"+objv.ToTraceString();


以下是輸出的 SQL 字串,msg 變數的內容:

SELECT
[Extent1].[BookID] AS [BookID],
[Extent1].[BookTitle] AS [BookTitle],
[Extent1].[BookISBN] AS [BookISBN],
[Extent1].[BookPrice] AS [BookPrice],
[Extent1].[BookAuthor] AS [BookAuthor],
[Extent1].[BookCategoryID] AS [BookCategoryID]
FROM [dbo].[Book] AS [Extent1]
WHERE [Extent1].[BookPrice] <> 700

沒有留言: