Remove(item) Not working Asp.Net Mvc

Asked

Viewed 47 times

0

I have a method that uses remove to remove from the list. but not for sure, not from the error, and neither does it remove.

first I go on the bench to get the object.

 var ctrl = _controleDeVisitaRepositorio.BuscarPorIdIncluso(controle.ControleDeVisitasId);

then I create a new list, with the Ids, which comes as parameter:

List<TipoDeSistemas> ts = new List<TipoDeSistemas>();
                //adicionar os objetos Tipo de sistemas a lista
                foreach (var t in sistemasComerciais)
                {
                    //buscaca o sistema no banco
                    var tt = _tipoDeSistemaRepositorio.BuscarPorId(t ?? Guid.Empty);
                    //adiciona a lista
                    ts.Add(tt);
                }

but my problem is here, it enters the if, but does not remove the item I want:

if (ts.Count() < lista2.Count())
                    {
                        var tsds = ts.Except(lista2).ToList();
                        foreach (var t in tsds)
                        {
                            ctrl.TipoDeSistemas.Remove(t);
                            db.Entry(ctrl).State = EntityState.Modified;
                            db.SaveChanges();
                        }

                    }

Where am I going wrong??

  • You should inform the EF that you are removing the TipoDeSistemas from the list. You can filter through the object and then "Set" as EntityState.Deleted

  • What I suggested solved?

No answers

Browser other questions tagged

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