Entity Framework Core - Example: Generated Queries
EF Core treats all SQL commands (Selects and modifications via SaveChanges) under the same logging category: Database.Command. As long as we have LogLevel.Information and LogTo enabled, they will appear in the console.
Here is an example that explicitly performs all 4 CRUD operations so we can see the log output for each one.
Complete CRUD Logging Example
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
public class Program
{
public static void Main()
{
// Setup: Ensure a fresh database exists
using (var setupContext = new AppDbContext())
{
setupContext.Database.EnsureDeleted();
setupContext.Database.EnsureCreated();
}
using var context = new AppDbContext();
// ==========================================
// 1. CREATE (INSERT)
// ==========================================
Console.WriteLine("\n--- 1. INSERT OPERATION ---");
var newUser = new User { Name = "John Doe", Email = "john@example.com" };
context.Users.Add(newUser);
// This triggers the INSERT SQL log
context.SaveChanges();
// ==========================================
// 2. READ (SELECT)
// ==========================================
Console.WriteLine("\n--- 2. SELECT OPERATION ---");
// This triggers the SELECT SQL log
var userToUpdate = context.Users
.Where(u => u.Email == "john@example.com")
.FirstOrDefault();
// ==========================================
// 3. UPDATE
// ==========================================
if (userToUpdate != null)
{
Console.WriteLine("\n--- 3. UPDATE OPERATION ---");
userToUpdate.Name = "Johnathan Doe"; // Modify property
// This triggers the UPDATE SQL log
context.SaveChanges();
}
// ==========================================
// 4. DELETE
// ==========================================
if (userToUpdate != null)
{
Console.WriteLine("\n--- 4. DELETE OPERATION ---");
context.Users.Remove(userToUpdate);
// This triggers the DELETE SQL log
context.SaveChanges();
}
Console.WriteLine("\n--- DEMO FINISHED ---");
}
}
// ==========================================
// Entity & Context Configuration
// ==========================================
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
public class AppDbContext : DbContext
{
public DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EfCoreCrudDemo;Trusted_Connection=True;")
// LOGGING CONFIGURATION
// ---------------------
// 1. Log to Console
// 2. Filter to only show SQL Commands (removes initialization noise)
// 3. Enable Sensitive Data to see the actual values in INSERT/UPDATE
.LogTo(
Console.WriteLine,
new[] { DbLoggerCategory.Database.Command.Name },
LogLevel.Information)
.EnableSensitiveDataLogging();
}
}
What we will see in the Console
When we run this, we will see output similar to this (timestamps removed for clarity):
1. The INSERT Log:
--- 1. INSERT OPERATION ---
Executed DbCommand (12ms) [Parameters=[@p0='john@example.com' (Size = 4000), @p1='John Doe' (Size = 4000)], CommandType='Text', CommandTimeout='30']
SET IMPLICIT_TRANSACTIONS OFF;
SET NOCOUNT ON;
INSERT INTO [Users] ([Email], [Name])
VALUES (@p0, @p1);
SELECT [Id]
FROM [Users]
WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();
2. The SELECT Log:
--- 2. SELECT OPERATION ---
Executed DbCommand (2ms) [Parameters=[@__targetEmail_0='john@example.com' (Size = 4000)], CommandType='Text', CommandTimeout='30']
SELECT TOP(1) [u].[Id], [u].[Email], [u].[Name]
FROM [Users] AS [u]
WHERE [u].[Email] = @__targetEmail_0
3. The UPDATE Log:
--- 3. UPDATE OPERATION ---
Executed DbCommand (8ms) [Parameters=[@p1='1', @p0='Johnathan Doe' (Size = 4000)], CommandType='Text', CommandTimeout='30']
SET IMPLICIT_TRANSACTIONS OFF;
SET NOCOUNT ON;
UPDATE [Users] SET [Name] = @p0
OUTPUT 1
WHERE [Id] = @p1;
4. The DELETE Log:
--- 4. DELETE OPERATION ---
Executed DbCommand (3ms) [Parameters=[@p0='1'], CommandType='Text', CommandTimeout='30']
SET IMPLICIT_TRANSACTIONS OFF;
SET NOCOUNT ON;
DELETE FROM [Users]
OUTPUT 1
WHERE [Id] = @p0;

