c# - Entity Framework 6 - How can I view the SQL that will be generated for an insert before calling SaveChanges -
in entity framework 6, possible view sql executed insert before calling savechanges?
using (var db = new stuffentities()){ db.things.add(new thing({...}); //can sql insert statement @ point? db.savechanges(); }
i'm familiar how generated sql query before execution so:
var query = db.thing.where(x => x.id == 9); console.writeline(query.tostring()); //this prints sql select statement
the query returns iqueryable<> whereas insert returns dbset , calling tostring on dbset prints standard object name.
another option (if understand question correctly), use idbcommandinterceptor
implementation, seemingly allows inspect sql commands before executed (i hedge words have not used myself).
something this:
public class commandinterceptor : idbcommandinterceptor { public void nonqueryexecuting( dbcommand command, dbcommandinterceptioncontext<int> interceptioncontext) { // whatever command.commandtext } }
register using dbinterception
class available in ef in context static constructor:
static stuffentities() { database.setinitializer<stuffentities>(null); // or have system.data.entity.infrastructure.interception.dbinterception.add(new commandinterceptor()); }
Comments
Post a Comment