Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Dbcontext not accessible for calling stored procedures in Application Project #10482


User avatar
0
kansoftware created

Prerequisites

Please answer the following questions before submitting an issue. YOU MAY DELETE THE PREREQUISITES SECTION.

  • What is your product version? 8.1
  • What is your product type (Angular or MVC)? MVC
  • What is product framework type (.net framework or .net core)? > netcore

If issue related with ABP Framework

  • What is ABP Framework version? After upgrading the .net core solutions to 5.0 and upgrading to latest ABP we are not able to get access Context for calling stored procedures. Please help how we can fix the same earlier it was working

4 Answer(s)
  • User Avatar
    0
    kansoftware created

    You can also refer to the project we shared earlier in that version it was working fine.

  • User Avatar
    0
    musa.demir created

    Can you please the base class of StoredProcedureCalling?

  • User Avatar
    0
    kansoftware created

    Hi please find the screenshot below taken from old project with old ABP 5.1:

  • User Avatar
    0
    BobIngham created

    Hi Kansoftware,

    I am upgrading from 6.80 to 10.4.0 and found I had to make the following changes to my SQLRepository:

            ... code removed for brevity
            
            /// <summary>
            /// Returns a list of abpUsers with links to entities in nuagecare
            /// </summary>
            /// <param name="tenantId">long</param>
            /// <param name="UserId">long</param>
            /// <param name="isDeleted">bool?</param>
            /// <returns>Task<IQueryable<GetStaffForKendoGrid_Result>></returns>
    
            public async Task<IQueryable<GetStaffForKendoGrid_Result>> GetStaffForKendoGrid(int tenantId, long userId, bool isDeleted)
            {
                await **EnsureConnectionOpen();**
    
                var parms = new SqlParameter[3];
                parms[0] = new SqlParameter("@TenantId", tenantId);
                parms[1] = new SqlParameter("@UserId", userId);
                parms[2] = new SqlParameter("@IsDeleted", isDeleted);
    
                using (var command =** CreateCommand("GetStaffForKendoGrid", CommandType.StoredProcedure, parms)**)
                {
                    using (var dataReader = await command.ExecuteReaderAsync())
                    {
                        var result = new List<GetStaffForKendoGrid_Result>();
    
                        while (dataReader.Read())
                        {
                            var model = new GetStaffForKendoGrid_Result();
                            
                            ... model population here
    
                            result.Add(model);
                        }
                        dataReader.Close();
                        return result.AsQueryable();
                    }
                }
            }
    
    ....
    
            #region helper methods
            /// <summary>
            /// 
            /// </summary>
            /// <param name = "commandText" ></param >
            /// < param name="commandType"></param>
            /// <param name = "parameters" ></param >
            /// < returns ></returns >
            DbCommand CreateCommand(string commandText, CommandType commandType, params SqlParameter[] parameters)
            {
                var command = **GetConnection().CreateCommand();**
    
                command.CommandText = commandText;
                command.CommandType = commandType;
                command.Transaction = GetActiveTransaction();
    
                foreach (var parameter in parameters)
                {
                    command.Parameters.Add(parameter);
                }
                return command;
            }
    
            /// <summary>
            /// Opens an SQL connection if one does not already exist.
            /// 20191020 - changed to OpenAsync()
            /// </summary>
            async Task EnsureConnectionOpen()
            {
                var connection = GetConnection();
    
                if (connection.State != ConnectionState.Open)
                {
                    await connection.OpenAsync();
                }
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            DbTransaction GetActiveTransaction()
            {
                return (DbTransaction)_transactionProvider.GetActiveTransaction(new ActiveTransactionProviderArgs
                {
                    {"ContextType", typeof(NuagecareDbContext) },
                    {"MultiTenancySide", MultiTenancySide }
                });
            }
            #endregion helper methods
    

    I also had to change my using statement from; using System.Data.SqlClient; to using Microsoft.Data.SqlClient; But I think that's more down to the fact that I was previously on aspnet framework 4.6.0 with donetcore 2.1.

    Hope that helps.