Home >Database >Mysql Tutorial >How Can I Log Generated SQL Queries from DbContext.SaveChanges() in EF6 Without External Libraries?

How Can I Log Generated SQL Queries from DbContext.SaveChanges() in EF6 Without External Libraries?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-03 12:22:39792browse

How Can I Log Generated SQL Queries from DbContext.SaveChanges() in EF6 Without External Libraries?

Logging Generated SQL from DbContext.SaveChanges()

This question seeks a solution to logging generated SQL queries from DbContext.SaveChanges() without external frameworks.

Answer:

In Entity Framework 6.0, this task is simplified by the Database class's Action Log property. By assigning it to a delegate, such as Console.WriteLine, you can log the SQL queries:

// Log SQL queries to the console
context.Database.Log = Console.WriteLine;

For more complex requirements, consider using an interceptor.

The above is the detailed content of How Can I Log Generated SQL Queries from DbContext.SaveChanges() in EF6 Without External Libraries?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn