简体   繁体   中英

Entity Framework (5) View parameters passed to generated SQL

How do I view the parameters (Bind Variables) passed to the generated SQL command created by Entity Framework (V5)?

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

The code above generated the following SQL and includes the bind variable __linq__0

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

How do I debug and view the value of this bind variable when debugging? I have seen answers which suggested enabling tracing, but there must be a way of checking these values in the Visual Studio debugger.

Thanks.

You need access to underline command, connection object, Which you can have by downloading EF sourcecode. EF wraps every thing for you. EF Codebase

If you're using Entity Framework 6 you could use the Interception/SQL Logging

Here is an example of the link posted below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM