Created
November 4, 2019 08:30
-
-
Save joe-oli/01bcd4263a3e64d1dbc2a4fdf67850e1 to your computer and use it in GitHub Desktop.
EF6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Applicable for EF 6.0 and above: | |
Any command sent from the EF to the database can now be logged. To view the generated queries from EF 6.x, use the DBContext.Database.Log property | |
What Gets Logged | |
- SQL for all different kinds of commands. For example: | |
- Queries, including normal LINQ queries, eSQL queries, and raw queries from methods such as SqlQuery. | |
- Inserts, updates, and deletes generated as part of SaveChanges | |
- Relationship loading queries such as those generated by lazy loading | |
- Parameters | |
- Whether or not the command is being executed asynchronously | |
- A timestamp indicating when the command started executing | |
- Whether or not the command completed successfully, failed by throwing an exception, or, for async, was canceled | |
- Some indication of the result value | |
- The approximate amount of time it took to execute the command. Note that this is the time from sending the command to getting the result object back. It does not include time to read the results. | |
Example: | |
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(); | |
} | |
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 | |
To log to an external file: | |
using (var context = new BlogContext()) | |
{ | |
using (var sqlLogFile = new StreamWriter("C:\\temp\\LogFile.txt")) | |
{ | |
context.Database.Log = sqlLogFile.Write; | |
var blog = context.Blogs.First(b => b.Title == "One Unicorn"); | |
blog.Posts.First().Title = "Green Eggs and Ham"; | |
context.SaveChanges(); | |
} | |
} | |
https://docs.microsoft.com/en-us/ef/ef6/fundamentals/logging-and-interception | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment