Separate entity from context

0

I'm working with Entity Framework 4.5.2, Visual Studio 2015

I have the following entity.

public class Especificacion
    {
        public int EspecificacionId { get; set; }
        public string Nombre { get; set; }

        public virtual ICollection<Opcion> Opciones { get; set; }
    }

At the moment of updating I only want to update the header but not the Options list, it occurs to me that I must separate from the Options context.

public void Actualizar(Especificacion entity)
        {
            using (var context = new BusinessContext())
            {
                foreach (var d in entity.Opciones)
                {
                    context.Entry(d).State = EntityState.Detached;
                }

                context.Entry(entity).State = EntityState.Modified;
                context.SaveChanges();
            }
        }

But in entity equal comes options, how can I solve it?

  

Error: {"Conflicting changes to the role have been detected   'Option_Specification_Target' of the relationship   'TecSoftware.Persistencia.Model.Opcion_Specificacion'. "}

For some reason there are problems in the model

Greetings!

Specifications

  • SpecificationId int
  • Name varchar (80)

Options

  • Option int
  • SpecificationId int
  • Name vaarchar (80)
asked by Pedro Ávila 08.09.2018 в 17:40
source

2 answers

1

I managed to solve it by sending it a simple entity, I mean without the option list

if (string.IsNullOrEmpty(txtEspecificacionId.Text))
            {
                var entity = new Especificacion()
                {
                    EspecificacionId = string.IsNullOrEmpty(txtEspecificacionId.Text)
                        ? 0
                        : Convert.ToInt32(txtEspecificacionId.Text),
                    Nombre = txtNombre.Text,
                    Opciones = _list
                };
                _saEspecificacion.Registrar(entity);
                txtEspecificacionId.Text = Convert.ToString(entity.EspecificacionId);
            }
            else
            {
                var entity = new Especificacion()
                {
                    EspecificacionId = string.IsNullOrEmpty(txtEspecificacionId.Text)
                        ? 0
                        : Convert.ToInt32(txtEspecificacionId.Text),
                    Nombre = txtNombre.Text
                };
                _saEspecificacion.Registrar(entity);
                txtEspecificacionId.Text = Convert.ToString(entity.EspecificacionId);
            }

That was it.

    
answered by 08.09.2018 / 22:10
source
1

What you should do is point to the particular property you want to modify, beats me that you want to change the name only, if I am understanding what you are looking for, the following code will give you a hand:

public void Actualizar(Especificacion entity)
{
    using (var context = new BusinessContext())
    {        
        context.NombreDeTuTabla.Attach(entity);
        // La actualización es la siguiente línea:
        entity.Opciones = null;
        context.Entry(entity).Property(x=>x.Nombre).IsModified = true;
        context.SaveChanges();
    }
}

More information: link

    
answered by 08.09.2018 в 19:37