I know there is a way to see SQL that will be executed when querying for data from a database. For example, when query is created I can use query.ToQueryString()
(based on this answer from SO):
var query = _context.Widgets.Where(w => w.IsReal && w.Id == 42);
var sql = query.ToQueryString();
But is there a way to see SQL that will be executed when SaveChanges()
is called without actually executing SaveChanges()
?
Code example that gets data from db and updates one field:
var context = new AppContext();
var author = context.Authors
.Include(a => a.Books)
.FirstOrDefault(a => a.AuthorId == 1);
// update some property
author.Books[0].BasePrice += 2;
// here I want to see SQL that will be generated to
// update the database when I call context.SaveChanges()
var sql = ...?