Insert Data Error EntityFramework Core

0

Sorry if the question is very obvious but I'm new to the subject and looking in Google I can not find anything about what happens.

I have the Next entity.

public class Equipo
    {
        public int Id { get; set; }
        public string Serial { get; set; }
        public string Descripcion { get; set; }
        public int IdTipoEquipo { get; set; }
        public int IdMarca { get; set; }
        public int IdRango { get; set; }
        public int IdResponsable { get; set; }
        public decimal PeriodoCalibracion { get; set; }
        public decimal Clase { get; set; }
        public string Patron { get; set; }
        public string ActivoFijo { get; set; }
        public string IdArticulo { get; set; }
        public int IdEstado { get; set; }
        public string UsuarioRegistro { get; set; }
        public DateTime FechaRegistro { get; set; } = DateTime.Now;
        public string UsuarioActualiza { get; set; }
        public DateTime FechaActualiza { get; set; } = DateTime.Now;
        public List<EquipoDetalle> Detalle { get; set; }
    }

As you see inside that entity goes another call detail that is this.

public class EquipoDetalle
    {
        public int Id { get; set; }
        [Column("IdEquipo")]
        public int EquipoId { get; set; }
        public DateTime Fecha { get; set; }
        public int IdLaboratorio { get; set; }
        public string Certificado { get; set; }
        public string Observacion { get; set; }
        public DateTime? ProxVerificacion { get; set; }
        public int IdMovimiento { get; set; }
        // public byte[] Imagen { get; set; }
        public int? IdCalibracion { get; set; }
        public int IdResponsable { get; set; }
        public int IdEstado { get; set; }
        public string UsuarioRegistro { get; set; }
        public DateTime FechaRegistro { get; set; } = DateTime.Now;
        public string UsuarioActualiza { get; set; }
        public DateTime FechaActualiza { get; set; } = DateTime.Now;
        public Programacion Programacion { get; set; }
    }

And the detail has another program call that is this.

public class Programacion
    {
        public int Id { get; set; }
        public string IdEmpleado { get; set; }
        public DateTime Fecha { get; set; }
        public int IdEquipoDetalle { get; set; }
        public int IdEstado { get; set; }
        public string UsuarioRegistro { get; set; }
        public DateTime FechaRegistro { get; set; }
        public string UsuarioActualiza { get; set; }
        public DateTime FechaActualiza { get; set; }
    }

That's where I have the problem, since the insert I want to make is only of Equipment and detail but not programming, programming goes null but at the moment I do the savechanges () it gives me an error and it's because programming go null, I do not know how to tell the entity to just insert the entities I want and not the last one.

I appreciate your guide since I am very new with entityFramework and I do not know how to look.

This is the insert method.

public int Add(Equipo item)
        {
            // Detalle
            item.Detalle = new List<EquipoDetalle>{
                    new EquipoDetalle{
                        Fecha= item.FechaRegistro,
                        IdLaboratorio = 1,
                        Observacion= "Registro de Equipo",
                        IdMovimiento = 1,
                        IdResponsable = item.IdResponsable,
                        IdEstado = item.IdEstado,
                        UsuarioRegistro= item.UsuarioRegistro,
                        UsuarioActualiza=item.UsuarioActualiza,
                        FechaRegistro=item.FechaRegistro,
                        FechaActualiza = item.FechaActualiza
                }
                    };
            try
            {
                _context.Equipo.Add(item);
                return _context.SaveChanges();
            }
            catch (Exception ex)
            {
                return 1;
            }

        }

As you can see, the item is full and the detail urges, if I comment on the programming property, it does not give me an error since it does not try to do the insert.

This is the relationship.

    
asked by Cristian 09.07.2017 в 02:38
source

1 answer

0

I recommend you check this link when you make a one-to-many relationship you have to put the list of the child class in the parent table and in the child put the parent class and the parent id as the attribute (Foreign key), if it is one-to-one in the parent class put the name of the other class and in the child class the name of the parent class and the Id (Foreign key).

By not setting the [Required] property, you should have no problem inserting a Detail Team if you do not have Programming

class Equipo{
  ...
public virtual ICollection<EquipoDetalle> EquipoDetalles {get;set;}
}

class EquipoDetalle{
...
public virtual Equipo Equipo {get;set;}
public int EquipoId {get;set;}

public virutal Programacion Programacion {get;set;}
}

class Programacion{
...
   public int EquipoDetalleId {get;set;}
   public virtual EquipoDetalle {get;set;}
}
    
answered by 13.07.2017 / 21:41
source