dibley1973 / StoredProcedureFramework

A .Net framework for calling stored procedures
MIT License
4 stars 2 forks source link

StoredProcedureFrameworkForEF - ExecuteStoredProcedure ignores the CommandTimeout setting applied to the DbContext #13

Open jadjare opened 5 years ago

jadjare commented 5 years ago

When a stored procedure is executed via the EF extensions the command timeout set against the DbContext is ignored. Only if you explicitly set the commandTimeoutOverride parameter when calling Execute or ExecuteFor will the timeout be respected.

This appears to be because the ExecuteStoredProcedure method in the Dibware.StoredProcedureFrameworkForEF.Extensions.DbContextExtensions class only utilises the database connection available against the db context.

The method currently looks like this:

        public static TResultSetType ExecuteStoredProcedure<TResultSetType, TParameterType>(
               this DbContext context,
               IStoredProcedure<TResultSetType, TParameterType> storedProcedure,
               int? commandTimeoutOverride = null,
               CommandBehavior commandBehavior = CommandBehavior.Default,
               SqlTransaction transaction = null)
            where TResultSetType : class, new()
            where TParameterType : class
        {
            if (storedProcedure == null) throw new ArgumentNullException("storedProcedure");

            storedProcedure.EnsureFullyConstructed();

            DbConnection connection = context.Database.Connection;

            return connection.ExecuteStoredProcedure(
                storedProcedure,
                commandTimeoutOverride,
                commandBehavior,
                transaction);
        }

It needs to be updated so that if the commandTimeoutOverrirde is not set it reverts to using the default value set up against the DbContext. For example:

        public static TResultSetType ExecuteStoredProcedure<TResultSetType, TParameterType>(
               this DbContext context,
               IStoredProcedure<TResultSetType, TParameterType> storedProcedure,
               int? commandTimeoutOverride = null,
               CommandBehavior commandBehavior = CommandBehavior.Default,
               SqlTransaction transaction = null)
            where TResultSetType : class, new()
            where TParameterType : class
        {
            if (storedProcedure == null) throw new ArgumentNullException("storedProcedure");

            storedProcedure.EnsureFullyConstructed();

            DbConnection connection = context.Database.Connection;

            return connection.ExecuteStoredProcedure(
                storedProcedure,
                commandTimeoutOverride ?? context.Database.CommandTimeout, // <<<<---- Use db command timeout if not overridden
                commandBehavior,
                transaction);
        }

I'm not sure if you're maintaining this project any longer, but let me know if you'd like to see the change as a pull request.

dibley1973 commented 5 years ago

Hey buddy, I'd love to see the change as a pull request. I assume that you have tested it well, anyway, knowing you! :D Are you guys using the nuget package, or just as it is? D. On Wednesday, 29 May 2019, 17:54:03 BST, jadjare notifications@github.com wrote:

When a stored procedure is executed via the EF extensions the command timeout set against the DbContext is ignored. Only if you explicitly set the commandTimeoutOverride parameter when calling Execute or ExecuteFor will the timeout be respected.

This appears to be because the ExecuteStoredProcedure method in the Dibware.StoredProcedureFrameworkForEF.Extensions.DbContextExtensions class only utilises the database connection available against the db context.

The method currently looks like this: public static TResultSetType ExecuteStoredProcedure<TResultSetType, TParameterType>( this DbContext context, IStoredProcedure<TResultSetType, TParameterType> storedProcedure, int? commandTimeoutOverride = null, CommandBehavior commandBehavior = CommandBehavior.Default, SqlTransaction transaction = null) where TResultSetType : class, new() where TParameterType : class { if (storedProcedure == null) throw new ArgumentNullException("storedProcedure");

        storedProcedure.EnsureFullyConstructed();

        DbConnection connection = context.Database.Connection;

        return connection.ExecuteStoredProcedure(
            storedProcedure,
            commandTimeoutOverride,
            commandBehavior,
            transaction);
    }

It needs to be updated so that if the commandTimeoutOverrirde is not set it reverts to using the default value set up against the DbContext. For example: public static TResultSetType ExecuteStoredProcedure<TResultSetType, TParameterType>( this DbContext context, IStoredProcedure<TResultSetType, TParameterType> storedProcedure, int? commandTimeoutOverride = null, CommandBehavior commandBehavior = CommandBehavior.Default, SqlTransaction transaction = null) where TResultSetType : class, new() where TParameterType : class { if (storedProcedure == null) throw new ArgumentNullException("storedProcedure");

        storedProcedure.EnsureFullyConstructed();

        DbConnection connection = context.Database.Connection;

        return connection.ExecuteStoredProcedure(
            storedProcedure,
            commandTimeoutOverride ?? context.Database.CommandTimeout, // <<<<---- Use db command timeout if not overridden
            commandBehavior,
            transaction);
    }

I'm not sure if you're maintaining this project any longer, but let me know if you'd like to see the change as a pull request.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.