EF Core - "A command is already in Progress"

Asked

Viewed 262 times

0

I am requesting a Function in the database where it returns the values to be saved, but before saving I need to make a check if there is no same data, when I do this check with EF core it returns me this error "A command is already in Progress:"

follows code:

       public void funcCompMargemDesvioPadrao()
        {   


            using (var connection = _context.Database.GetDbConnection())
            {

                using (var command = connection.CreateCommand())
                {
                    connection.Open();
                    command.CommandText = "select * from computer_vision.funccompmargemdesviopadrao()";

                    using (var data = command.ExecuteReader())
                    {
                        if (data.HasRows)
                        {
                            while (data.Read())
                            {
                                var objDTO = new Compiled_daily_pass_registerDTO();

                                objDTO.entity_id = Convert.ToInt32(data["id"]);
                                objDTO.pass_date_time = Convert.ToDateTime(data["date_time"]);
                                objDTO.qty_daily_pass = Convert.ToDouble(data["qty_comp"]);
                                objDTO.excluded = false;
                                objDTO.updated_at = DateTime.Now;

                                // AQUI QUE RETORNAR O ERRO
                                compiled_daily_pass_register compiled_day = _context.compiled_daily_pass_register.SingleOrDefault(
                                x => x.pass_date_time == objDTO.pass_date_time && x.entity_id == objDTO.entity_id);

                                objDTO.created_at = compiled_day.created_at;

                                compiled_daily_pass_register obj = new compiled_daily_pass_register();
                                obj = _mapper.Map<Compiled_daily_pass_registerDTO, compiled_daily_pass_register>(objDTO, obj);

                                if (compiled_day != null)
                                {
                                    _context.compiled_daily_pass_register.Remove(compiled_day);
                                    _context.compiled_daily_pass_register.Add(obj);
                                    _context.SaveChanges();
                                }

                            }
                        }
                    }
                }
                connection.Close();

            }
  • 1

    _context.SaveChanges(); has such a command in the middle of a DataReader will go wrong even ... is because of this. First you create the whole list and then you execute the command referring to the EF. In my view it seems that you too are going the wrong way, as I have no way of knowing and stating 100% ...

  • Hello @Virgilionovic, thanks for answering me, so I need to open another connection to the bank? or save the records in a list and after that, use them for the query?

  • 1

    You have to generate the list first and then do the operation (I don’t know how good this is, I can’t see your problem in real) because from the moment you take the connection in a common way and within a DataReader if you can’t give a SaveChanges() the EF itself does not let. Then create the list and exit that using then you do the operation normally. But still I think it’s bad code, could do it maybe better is that I can’t really see what you want to do, it’s too much code

  • @Virgilionovic right, I did something very close to what you said, I added in a list of obj the data I search in dataReader, then when it finishes this loop, out of it I do the other select to check if there is equal data, otherwise it exists it creates a new one and if it exists it does an update

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.