Add SQL to Generated Entity Framework SQL RRS feed

  • Question

  • Is there way to append custom SQL to the SQL generated from the Entity Framework.  The reason I am asking is because an ERP system that I am working with requires some underlying SQL to be called before you can execute any Select/Insert/Update statements. Right now all database interactions are done via stored procedures which I can continue to use going forward, but I could reduce the number of stored procedures I have to maintain if I can append a basic SQL statement before the SQL generated from the Entity Framework.

    For example say Entity framework creates this SQL statement:

    Select FirstName From Customer;

    However I need it to generate SQL like this

    ##Custom SQL Statement##;

    Select FirstName From Customer;

    Without first executing the Custom SQL Statement first, the select statement won't work. This a requirement by the ERP system when executing SQL against it.

    Thanks for Your Help


    Monday, February 17, 2020 3:07 PM


  • You can create an Interceptor: https://www.entityframeworktutorial.net/entityframework6/database-command-interception.aspx 

    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    • Marked as answer by B1ades Tuesday, February 25, 2020 5:20 PM
    Monday, February 17, 2020 6:15 PM