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
Release: 2025-01-03 12:22:39
Original
789 people have browsed it

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;
Copy after login

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 of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template