簡體   English   中英

實體框架(5)查看傳遞給生成的SQL的參數

[英]Entity Framework (5) View parameters passed to generated SQL

如何查看傳遞給由實體框架(V5)創建的生成的SQL命令的參數(綁定變量)?

public IEnumerable<SearchItem> Search(string searchTerm)
{
    return DbSet.Where(p => p.ItemId.Contains(searchTerm)).ToList();
}

上面的代碼生成了以下SQL,並包含了綁定變量__linq__0

SELECT 
     "Extent1"."ITEM" AS "ITEM", 
FROM "TableName" "Extent1"
WHERE UPPER("Extent1"."ITEM") LIKE :p__linq__0

調試時如何調試和查看此綁定變量的值? 我已經看到了建議啟用跟蹤的答案,但是必須有一種在Visual Studio調試器中檢查這些值的方法。

謝謝。

您需要訪問下划線命令,連接對象,可以通過下載EF源代碼獲得該命令。 EF為您包裝了所有東西。 EF代碼庫

如果您使用的是Entity Framework 6,則可以使用Interception / SQL Logging

這是下面發布的鏈接的示例

using (var context = new BlogContext()) 
{ 
    context.Database.Log = Console.Write; 

    var blog = context.Blogs.First(b => b.Title == "One Unicorn"); 

    blog.Posts.First().Title = "Green Eggs and Ham"; 

    blog.Posts.Add(new Post { Title = "I do not like them!" }); 

    context.SaveChangesAsync().Wait(); 
}
This will generate the following output:

SELECT TOP (1)
    [Extent1].[Id] AS [Id],
    [Extent1].[Title] AS [Title]
    FROM [dbo].[Blogs] AS [Extent1]
    WHERE (N'One Unicorn' = [Extent1].[Title]) AND ([Extent1].[Title] IS NOT NULL)
-- Executing at 10/8/2013 10:55:41 AM -07:00
-- Completed in 4 ms with result: SqlDataReader

SELECT
    [Extent1].[Id] AS [Id],
    [Extent1].[Title] AS [Title],
    [Extent1].[BlogId] AS [BlogId]
    FROM [dbo].[Posts] AS [Extent1]
    WHERE [Extent1].[BlogId] = @EntityKeyValue1
-- EntityKeyValue1: '1' (Type = Int32)
-- Executing at 10/8/2013 10:55:41 AM -07:00
-- Completed in 2 ms with result: SqlDataReader

UPDATE [dbo].[Posts]
SET [Title] = @0
WHERE ([Id] = @1)
-- @0: 'Green Eggs and Ham' (Type = String, Size = -1)
-- @1: '1' (Type = Int32)
-- Executing asynchronously at 10/8/2013 10:55:41 AM -07:00
-- Completed in 12 ms with result: 1

INSERT [dbo].[Posts]([Title], [BlogId])
VALUES (@0, @1)
SELECT [Id]
FROM [dbo].[Posts]
WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity()
-- @0: 'I do not like them!' (Type = String, Size = -1)
-- @1: '1' (Type = Int32)
-- Executing asynchronously at 10/8/2013 10:55:41 AM -07:00
-- Completed in 2 ms with result: SqlDataReader

http://msdn.microsoft.com/en-US/data/dn469464

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM