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.