Base solution for your next web application
Open Closed

ExecuteReader requires the command to have a transaction #8038


User avatar
0
farhantufail created
      public class PayrollManager : ApplicationServiceBase, IDomainService
        {
        public SalarySheet CreateSalary(int employeeCode,int periodId, int month, int year)
            {

              SalaryPeriod salaryPeriod = _salaryPeriodRepository.FirstOrDefault(x => x.Id == periodId);
                int startDay = salaryPeriod.FromDate.Day;
                int endDay = salaryPeriod.ToDate.Day;
             decimal daysRemoval = _timeCardDetailCustomRepository.CalculateDaysremoval(startDate, endDate, employeeCode);
            }
          }

//Custom Repository

            public class TimeCardDetailRepository : ApplicationRepositoryBase<TimeCardDetail>,ITimeCardDetailRepository
        {
            IConfigurationRoot configuration = AppConfigurations.Get(WebContentDirectoryFinder.CalculateContentRootFolder(), addUserSecrets: true);



            public TimeCardDetailRepository(IDbContextProvider<ApplicationContext> dbContextProvider)
                    : base(dbContextProvider)
            {

            }


            public decimal CalculateDaysremoval(DateTime startDate,DateTime endDate,int employeeCode)
            {

                string sql = "SELECT ISNULL(SUM(CASE WHEN mark = 1 and TotalHours >= 4  THEN 0.5 WHEN mark = 1 and TotalHours < 4  THEN 1  end),0)as removalDays  FROM dbo.tabel WHERE EID = @employeeId";

                decimal result = 0;
                using (var con=GetDbContext().Database.GetDbConnection())
                {
                      using (var command = con.CreateCommand())
                        {

                            command.CommandText = sql;

                            command.Parameters.Add(new SqlParameter("@employeeId", employeeCode));



                            using (var dr = command.ExecuteReader())
                            {

                                if (dr.Read())
                                {
                                    result = Convert.ToDecimal(dr["removalDays"]);
                                }
                            }
                        }



                }

                return result;
            }




        }
        
        
        
        
        

It throws this exception.

My Question is that how can we Pass the transection from Domain Service to Custom repository Method ? In Cutome repository method we want to use Pure Ado.net because we have a alot of views from getting data and we can not map every view to EF Core keyless Entity type.

As Domain Services already Implemnet UnitOfWork by default in ABP and we jsut want to pass the transection used by UnitOfWork in Cutom repo methods or any work around to implement this behaviour?


4 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    Is the _timeCardDetailCustomRepository variable an ITimeCardDetailRepository interface or a TimeCardDetailRepository class?

    Please share the full code.

  • User Avatar
    0
    farhantufail created

    yes

    _timeCardDetailCustomRepository variable an ITimeCardDetailRepository interface

    ` public interface ITimeCardDetailRepository : IRepository

        decimal CalculateDaysremoval(DateTime startDate, DateTime endDate, int employeeCode);
      
    }
    

    **Above is the Interface and blew is the implimentation **


    public class TimeCardDetailRepository : ApplicationRepositoryBase

        public TimeCardDetailRepository(IDbContextProvider<ApplicationlDbContext> dbContextProvider)
                : base(dbContextProvider)
        {
            
        }
        
         public decimal CalculateDaysremoval(DateTime startDate,DateTime endDate,int employeeCode)
        {
           
            string sql = "SELECT ISNULL(SUM(CASE WHEN mark = 1 and TotalHours >= 4  THEN 0.5 WHEN mark = 1 and TotalHours < 4  THEN 1  end),0)as removalDays  FROM dbo.tabel WHERE EID = @employeeId";
        
            decimal result = 0;
            using (var con=GetDbContext().Database.GetDbConnection())
            {
                  using (var command = con.CreateCommand())
                    {
    
                        command.CommandText = sql;
                      
                     
                        command.Parameters.Add(new SqlParameter("@employeeId", employeeCode));
                        command.Parameters.Add(new SqlParameter("@fromDate", startDate));
                        command.Parameters.Add(new SqlParameter("@toDate", endDate));
    
                      
                        using (var dr = command.ExecuteReader())
                        {
    
                            if (dr.Read())
                            {
                                result = Convert.ToDecimal(dr["removalDays"]);
                            }
                        }
                    }
                   
                
               
            }
           
            return result;
        }
    }
    

    }

  • User Avatar
    0
    maliming created
    Support Team

    hi

    Try command.Transaction = Transaction;

    command.CommandText = sql;
    
    command.Transaction = Transaction;
    
    command.Parameters.Add(new SqlParameter("@employeeId", employeeCode));
    command.Parameters.Add(new SqlParameter("@fromDate", startDate));
    command.Parameters.Add(new SqlParameter("@toDate", endDate));
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    This issue is closed because it has not had recent activity for a long time.