1

Is there a way for me to override Database.ExecuteSqlCommand so that I can force a stored procedure to run (along with whatever the actual sql is) every time I call it?

12
  • What do you mean by "so that I can force a stored procedure to run (along with whatever the actual sql is) every time I call it"? Can you show us what your SQL is? Commented Jan 14, 2020 at 14:38
  • I believe it means to run a SP before executing Raw SQL code with ExecuteSqlCommand Commented Jan 14, 2020 at 14:40
  • I doubt that it is possible. Database Property of DbContext doesn't have setter, so you can't replace Database instance with your implementation. Commented Jan 14, 2020 at 14:42
  • you can pretty easily override the SaveChanges method but this will only help you run a sproc on create/update/delete, not retrieving data. I can provide an example if you think this would get you where you need to be Commented Jan 14, 2020 at 14:45
  • What version of EF? See learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/… and learn.microsoft.com/en-us/ef/ef6/fundamentals/… Commented Jan 14, 2020 at 14:47

4 Answers 4

1

In short, you can't. At least not through overriding. For you to override a method it has to originally be abstract or virtual.

You can create a method in your DbContext class to deal with the execution of your stored procedure before executing your query instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Are you talking about adding an extension method?
It could be, but an extension would only be used to prepare a query (attaching SP there). I was talking about creating a full method in a Context class.
1

No, you cannot override Database.ExecuteSqlCommand because those methods are not virtual. Also, usually it's not a good idea to call Database.ExecuteSqlCommand directly from services/controllers. Instead you could have another level of abstraction where you could have method which will call stored procedure before executing SQL command.

    public int RunSqlCommand(string sql, params object[] parameters)
    {
        _database.ExecuteSqlCommand("YourStoredProcedure @p0", parameters: new[] {"Parameter1"});
        _database.ExecuteSqlCommand (sql, parameters);
    }

Comments

0

What I eventually went with to have the least minimal impact to existing code was to just use the connection interceptor. Whenever a connection is opened I ran my stored procedure before anything else:

public class myInterceptor : IDbConnectionInterceptor { public void Opened(DbConnection connection, DbConnectionInterceptionContext interceptionContext) { //do my SP here } }

Comments

-1

A few ideas that come into my mind would be to implement your own method (override or extend Database), to 'force' the execution of the stored procedure before and then, run the SQL that was passed as a parameter. Some other option would be to use Dapper and call your procedure and then your SQL.

1 Comment

How do you propose to override a non-virtual method?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.